标签:
中国剩余定理。
读入优化比scanf慢了4ms,迷
1 /*by SilverN*/ 2 #include<algorithm> 3 #include<iostream> 4 #include<cstring> 5 #include<cstdio> 6 #include<cmath> 7 #define LL long long 8 using namespace std; 9 const int mxn=15; 10 LL a[mxn],b[mxn]; 11 int n; 12 LL exgcd(LL a,LL b,LL &x,LL &y){ 13 if(!b){ 14 x=1;y=0; 15 return a; 16 } 17 LL res=exgcd(b,a%b,x,y); 18 LL t=x;x=y;y=t-a/b*x; 19 return res; 20 } 21 LL clc(){ 22 LL M=1; 23 LL res=0; 24 int i,j;LL x,y; 25 for(i=1;i<=n;i++){M*=a[i];} 26 for(i=1;i<=n;i++){ 27 LL tmp=M/a[i]; 28 exgcd(tmp,a[i],x,y); 29 res=(res+tmp*x*b[i])%M; 30 } 31 res=(res+M)%M; 32 return res; 33 } 34 int main(){ 35 scanf("%d",&n); 36 int i,j; 37 for(i=1;i<=n;i++){ 38 scanf("%d%d",&a[i],&b[i]); 39 } 40 LL ans=clc(); 41 printf("%lld\n",ans); 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5913213.html