模方程的合并
jys
posted @ Dec 03, 2013 02:48:50 AM
in 未分类
, 778 阅读
int a[NN],b[NN],n; //ax=b(mod c) int solve() { int i,k,d,tmp,tmp2; for(i=2;i<=n;i++) { tmp=a[i-1]-a[i]; if(tmp<0)tmp=b[i-1]-(-tmp)%b[i-1]; d=gcd(b[i],b[i-1]); if(tmp%d>0)return -1; exgcd(b[i]/d,b[i-1]/d,k,tmp2); k*=tmp/d; if(k<0)k=b[i-1]-(-k)%b[i-1]; else k%=b[i-1]; //printf("%d * %d = %d mod %d\n",b[i],k,tmp,b[i-1]); //printf("x= %d * %d + %d\n",k,b[i],a[i]); a[i]=k*b[i]+a[i]; b[i]=lcm(b[i],b[i-1]); a[i]%=b[i]; //printf("x mod %d = %d\n",b[i],a[i]); } return a[n]; }