F-strange way to express Integers
Time limit:1000 ms
Memory limit:131072kb
64bit Io format:% I64d & % i64usubmit status
Description
Elina is reading a book written by rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:
ChooseKDifferent Positive IntegersA1,A2 ,...,AK. For some non-negativeM, Divide it by everyAI(1 ≤I≤K) To find the remainderRi. IfA1,A2 ,...,AKAre properly chosen, m can be determined, then the pairs (AI,Ri) Can be used to expressM.
"It is easy to calculate the pairs fromM, "Said Elina." But how can I findMFrom 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 integerK.
- Lines 2 ~K+ 1: Each contains a pair of IntegersAI,Ri(1 ≤I≤K).
Output
Output the non-negative integerMOn 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.
The meaning of the question is very simple. Each group of K group a r represents x branch R (mod A). Note that all A is not necessarily the same, because a does not interact with each other, it cannot directly use the Chinese surplus theorem. After reading a lot of information, I feel that the mathematician's thinking is not understandable. I still need to write the computation process on my own.
First, calculate the two groups of X forward r1 (mod A1); X forward R2 (mod A2); Define the variable K1 K2 to get x = K1 * A1 + R1; X = k2 * A2 + R2; obtained from the above equation K1 * A1 + R1 = k2 * A2 + R2; converted to K1 * a1 = (R2-R1) + K2 * A2; for Left and Right modulo A2, because (K2 * A2) % S2 = 0, the equation is converted to K1 * A1 partition (R2-R1) (mod A2 ); extended Euclidean can be used to solve the value of K1 (to determine whether there is a value of K1), bringing K1 back to X1 = K1 * A1 + R1; A special solution satisfying both {x = K1 * A1 + R1; X = k2 * A2 + R2;} is obtained. Therefore, X forward x1 (mod lcm (a1, a2 )); that is, X round (K1 * A1 + R1) (mod (A1 * A2/D); in this way, the two coincidences are converted into one, merge K equations into one, and use Extended Euclidean to obtain the smallest positive solution x
#include <cstdio>#include <cstring>#include <algorithm>#define LL __int64using namespace std;void gcd(LL a,LL b,LL &d,LL &x,LL &y){ if(b == 0) { d = a ; x = 1 ; y = 0 ; } else { gcd(b,a%b,d,y,x); x = -x ; y = -y ; y += (a/b)*x ; } return ;}int main(){ LL k , a1 , a2 , r1 , r2 , d , x , y ; while(scanf("%I64d", &k)!=EOF) { LL flag = 1 ; scanf("%I64d %I64d", &a1, &r1); k-- ; while(k--) { scanf("%I64d %I64d", &a2, &r2); gcd(a1,a2,d,x,y); if( (r2-r1)%d ) flag = 0 ; if( flag ) { x = (r2-r1)/d*x ; y = a2/d ; x = ( x%y +y)%y ; r1 = x*a1 + r1 ; a1 = (a1*a2)/d ; } } gcd(1,a1,d,x,y); if( r1%d ) flag = 0 ; if(flag == 0) printf("-1\n"); else { x = r1/d*x ; y = a1 / d ; x = ( x%y+y )%y ; printf("%I64d\n", x); } } return 0;}