标签:logs 线性 scan pre blog can nbsp main long
有错。。
1 #include<cstdio> 2 #define ll long long 3 4 ll a,b,c; 5 ll x,y; 6 7 ll r[1010],m[1010]; 8 9 void exgcd(ll a,ll b,ll& d,ll &x, ll &y) 10 { 11 if(!b) 12 { 13 d=a; 14 x=1; 15 y=0; 16 } 17 else 18 { 19 exgcd(b,a%b,d,y,x); 20 y-=x*(a/b); 21 } 22 } 23 24 ll crt(ll m[],ll r[],int n) 25 { 26 ll M=m[1],R=r[1],x,y,d; 27 for(int i=2;i<=n;i++) 28 { 29 exgcd(M,m[i],d,x,y); 30 if( (r[i]-R)%d ) return -1; 31 x=(r[i]-R)/d*x%(m[i]/d); 32 R+=M*x; 33 M=M*m[i]/d; 34 R%=M; 35 } 36 return R>0?R:R+M; 37 } 38 39 int main() 40 { 41 int n; 42 while(scanf("%d",&n)!=EOF) 43 { 44 for(int i=1;i<=n;i++) 45 { 46 scanf("%lld%lld",&m[i],&r[i]); 47 } 48 ll ans=crt(m,r,n); 49 printf("%lld\n",ans); 50 } 51 52 }
标签:logs 线性 scan pre blog can nbsp main long
原文地址:http://www.cnblogs.com/yijiull/p/6705383.html