Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 19509 | Accepted: 6592 |
Description
Choose k different positive integers a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) can be used to express m.
“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”
Since Elina is new to programming, this problem is too difficult for her. Can you help her?
Input
The input contains multiple test cases. Each test cases consists of some lines.
- Line 1: Contains the integer k.
- Lines 2 ~ k + 1: Each contains a pair of integers ai, ri (1 ≤ i ≤ k).
Output
Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.
Sample Input
28 711 9
Sample Output
31
Hint
All integers in the input and the output are non-negative and can be represented by 64-bit integral types.
模不互素不能用CRT,所有就有了我解线性方程组的方法?
但是有个关键步骤不太懂。。只是会用这个板子。。
1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #define ll long long 9 #define dscan(a) scanf("%d",&a)10 #define mem(a,b) memset(a,b,sizeof a)11 using namespace std;12 #define MAXL 110513 #define Endl endl14 #define maxn 100000515 ll x,y;16 inline ll read()17 {18 ll x=0,f=1;char ch=getchar();19 while(ch<'0'||ch>'9') { if(ch=='-') f=-1;ch=getchar();}20 while(ch>='0'&&ch<='9') {x=10*x+ch-'0';ch=getchar();}21 return x*f;22 }23 ll exgcd(ll a,ll b,ll &x,ll &y){24 if(b==0) {25 x=1;y=0;return a;26 }27 ll d=exgcd(b,a%b,x,y);28 ll temp=x;29 x=y;30 y=temp-a/b*y;31 return d;32 }33 int main()34 {35 ll k,a,b,c,d;36 while(~scanf("%lld",&k))37 {38 cin>>a>>b;39 //cout<<"hhh"<