http://poj.org/problem?id=2891
Solutions to linear equations.
Compared to the pit father, the data is relatively large, easily overflow.
1 Program poj2891;2 3 varM:int64;4 5Ar:Array[1..30000000] ofInt64;6 7 Ans,x,y,lcm:int64;8 9 Ten Procedure init; One A varI,j:longint; - - begin the -m:=0; - - Readln (m); + - fori:=1 toM Do + A readln (A[i],r[i]); at - End; - - -Function EXTENDED_GCD (A,b:int64;varX,y:int64): Int64; - in varK:int64; - to begin + - ifb=0 Then begin the *extended_gcd:=A; $ Panax Notoginsengx:=1; y:=0; - the End + A Else begin the +EXTENDED_GCD:=EXTENDED_GCD (b,aMoDb,x,y); - $K:=x;x:=y;y:=k-(ADivb) *y; $ - End; - the End; - Wuyi the Procedure Print (ans:int64); - Wu begin - About writeln (ans); $ - End; - - A Procedure main; + the varI:longint; - $ D,ai,ri,k:int64; the the begin the theans:=0; - inai:=a[1];ri:=r[1]; the the fori:=2 toM Do About the begin the thed:=EXTENDED_GCD (ai,a[i],x,y); + -Lcm:=a[i]*aiDivD; the Bayi if(R[i]-ri)MoDD<>0 Then beginPrint (-1); exit;End; the theK:= ((xMoDA[i]) * (R[i]-ri)MoDA[i])DivD //This surplus is very important, no words will overflow, in this WA for a long time - -Ri:= ((ri+ai*k)MoDLCM+LCM)MoDLCM; the theai:=LCM; the the End; - the print (RI); the the End;94 the the Begin the 98Assign (input,'input.in'); reset (input); About -Assign (output,'Output.out'); rewrite (output);101 102 while not(EOF) Do103 104 begin the 106 Init;107 108 ifM<>0 Thenmain;109 the End;111 the Close (Input); close (Output);113 the End.
POJ 2891 Strange to Express integers 2012-09-05