A positive integer K, gives the result of some prime number K Mod, meet the conditions required minimum K. E.g., K% 2 = 1, K% 3 = 2, K% 5 = 3. Eligible minimum of K = 23.
Entry
Line 1: The number N represents the number 1 and the prime analog input later. (2 <= N <= 10 ) of 2 - N + 1 lines, each P is the number 2 and M, separated by a space, P is a prime number, M is the result of K% P. (2 <= P <= 100 , 0 <= K <P)
Export
Output qualified smallest K. All data were less than 10 K ^ 9.
SAMPLE INPUT
3
2 1
3 2
5 3
Sample Output
23
Basics:
x ≡a1 (v m1) x ≡a2 (v m2) ...... x ≡an (v mn);
Wherein, m1.m2, m3 ....., mn pairwise relatively prime integers
令M=(m1*m2*m3....*mn);
Mi = M / mi, ti is an equation Mi * ti a solution ≡1 (mod mi) of
Then x has an integer solution, the solution is x = sum 1 ~ n (ai * Mi * ti);
Certify as follows:
Because Mi = m / mi mi other than multiples of, so if all the modulus = [Sigma substituting X (n-~. 1) * AI * Mi Ti , the original equation established
Can be understood as each aiMiti i only contribute to this equation, have no effect on other equations
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 ll arr[15],brr[15]; 5 int n; 6 ll X,Y; 7 ll gdc(ll a,ll b) { return b==0?a:gdc(b,a%b); } 8 void exgdc(ll a,ll b,ll &X,ll &Y){ 9 if(b==0){ 10 X=1,Y=0;return; 11 } 12 exgdc(b,a%b,X,Y); 13 ll temp=X; 14 X=Y; 15 Y=temp-a/b*Y; 16 } 17 18 19 int main(){ 20 ios::sync_with_stdio(false); 21 cin>>n; 22 ll lcm=1; 23 for(int i=1;i<=n;i++){ 24 cin>>arr[i]>>brr[i]; 25 lcm*=arr[i]; 26 } 27 ll res=0; 28 for(int i=1;i<=n;i++){ 29 exgdc(lcm/arr[i],arr[i],X,Y); 30 X=(X+arr[i])%(arr[i]); 31 res=(res+brr[i]*X*lcm/arr[i]); 32 } 33 cout << res%lcm << endl; 34 return 0; 35 }