1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| void exgcd(int a,int b,int &x,int &y) { if(b==0){ x=1; y=0; return;} exgcd(b,a%b,x,y); int tp=x; x=y; y=tp-a/b*y; }
int china() { int ans=0,lcm=1,x,y; for(int i=1;i<=k;++i) lcm*=b[i]; for(int i=1;i<=k;++i) { int tp=lcm/b[i]; exgcd(tp,b[i],x,y); x=(x%b[i]+b[i])%b[i]; ans=(ans+tp*x*a[i])%lcm; } return (ans+lcm)%lcm; }
lnt exgcd(lnt a,lnt b,lnt &x,lnt &y) { if(b==0){x=1;y=0;return a;} lnt gcd=exgcd(b,a%b,x,y); lnt tp=x; x=y; y=tp-a/b*y; return gcd; }
lnt excrt() { lnt x,y,k; lnt M=bi[1],ans=ai[1]; for(int i=2;i<=n;i++) { lnt a=M,b=bi[i],c=(ai[i]-ans%b+b)%b; lnt gcd=exgcd(a,b,x,y),bg=b/gcd; if(c%gcd!=0) return -1;
x=mul(x,c/gcd,bg); ans+=x*M; M*=bg; ans=(ans%M+M)%M; } return (ans%M+M)%M; }
|