Title: http://acm.hdu.edu.cn/showproblem.php?pid=3579
Problem analysis: To find the smallest solution x of a linear congruence equation, it is important to note that if x equals 0, an integer interval LCM (a1,a2,a3,... An) is required to add a general solution to the equation group.
There's nothing else to watch out for.
#include <iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<math.h>using namespacestd;__int64 a,b,c,d;__int64 x,y;__int64 gcd (__int64 A,__int64 b) {returnb==0? A:GCD (b,a%B);}voidExtend (__int64 a,__int64 b,__int64 &d,__int64 &x1,__int64 &y1) { if(b==0) {x1=1; Y1=0; D=A; return ; } Extend (B,a%b,d,x1,y1); __int64 Temp=X1; X1=Y1; Y1=temp-(A/b) *Y1; return ;}intMain () {__int64 s[ -],e[ -]; __int64 A1,R1,A2,R2,LCM; __int64 T; intm,k=0; scanf ("%i64d",&T); while(t--) {scanf ("%d",&m); LCM=1; for(intI=1; i<=m; i++) {scanf ("%i64d",&R[i]); LCM=LCM/GCD (Lcm,s[i]) *s[i];//to a certain extent can prevent explosion type (LCM*S[I]/GCD ()) } for(intI=1; i<=m; i++) {scanf ("%i64d",&E[i]); } BOOLIfhave=true; A1=s[1],r1=e[1]; for(__int64 i=2; i<=m; i++) {A2=s[i],r2=E[i]; A=A1; b=A2; C=r2-R1; Extend (a,b,d,x,y); if(c%d) {Ifhave=false; Break; } __int64 T=b/D; X= (x* (C/D)%t+t)%T; X=a1*x+R1; A1=a1* (a2/d); R1=X; } printf ("Case %d:",++K); if(!Ifhave) {printf ("-1\n"); Continue; } if(r1==0) R1+=Lcm; printf ("%i64d\n", R1); } return 0;}
Hdu3579:hello Kiki (solution of one-element linear congruence equation set)