标签:任务 ble 代码 整数 tput 接下来 思想 inline stdin
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 typedef long long L; 6 L n; 7 int m; 8 L a[11],mod[11]; 9 L M(1),ans(0); 10 inline void extend_gcd(L a,L b,L &x,L &y){ 11 if(b==0){ 12 x=1; 13 y=0; 14 return; 15 } 16 extend_gcd(b,a%b,x,y); 17 L tmp(x); 18 x=y; 19 y=tmp-(a/b)*y; 20 } 21 inline L CRT(L a[],L m[],int n){ 22 for(int i=1;i<=n;i++) 23 M*=m[i]; 24 for(int i=1;i<=n;i++){ 25 L x,y; 26 L Mi(M/m[i]); 27 extend_gcd(Mi,m[i],x,y); 28 ans=(ans+M+Mi*x*a[i])%M; 29 } 30 //if(ans<0) 31 // ans+=M; 32 return ans; 33 } 34 inline int gg(){ 35 freopen("HanXin.in","r",stdin); 36 freopen("HanXin.out","w",stdout); 37 scanf("%lld%d",&n,&m); 38 for(int i=1;i<=m;i++) 39 scanf("%lld%lld",&mod[i],&a[i]); 40 L ans(CRT(a,mod,m)); 41 if(ans>n){ 42 puts("-1"); 43 return 0; 44 } 45 while(ans<n) 46 ans+=M; 47 ans-=M; 48 printf("%lld",n-ans); 49 } 50 int k(gg()); 51 int main(){;}
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 typedef long long L; 6 int m; 7 L a[21],mod[21]; 8 inline L gcd(L a,L b){ 9 return a%b?gcd(b,a%b):b; 10 } 11 inline L ext_gcd(L a,L b,L &x,L &y){ 12 if(b==0){ 13 x=1; 14 y=0; 15 return a; 16 } 17 L gcd(ext_gcd(b,a%b,x,y)); 18 L tmp(x); 19 x=y; 20 y=tmp-(a/b)*y; 21 return gcd; 22 } 23 inline L ny(L a,L b){ 24 L x,y; 25 L gcd(ext_gcd(a,b,x,y)); 26 if(gcd!=1) 27 return -1; 28 return (x%b+b)%b; 29 } 30 inline bool merge(L a1,L m1,L a2,L m2,L &a3,L &m3){ 31 L d(gcd(m1,m2)); 32 L c=a2-a1; 33 if(c%d) 34 return false; 35 c=(c%m2+m2)%m2; 36 m1/=d; 37 m2/=d; 38 c/=d; 39 c*=ny(m1,m2); 40 c%=m2; 41 c*=m1*d; 42 c+=a1; 43 m3=m1*m2*d; 44 a3=(c%m3+m3)%m3; 45 return true; 46 } 47 L CRT(L a[],L m[],int n){ 48 L a1(a[1]),m1(m[1]); 49 for(int i=2;i<=n;i++){ 50 L a2(a[i]),m2(m[i]),a3,m3; 51 if(!merge(a1,m1,a2,m2,a3,m3)) 52 return -1; 53 a1=a3; 54 m1=m3; 55 } 56 return (a1%m1+m1)%m1; 57 } 58 inline int gg(){ 59 freopen("weakhanxin.in","r",stdin); 60 freopen("weakhanxin.out","w",stdout); 61 scanf("%d",&m); 62 for(int i=1;i<=m;i++) 63 scanf("%lld%lld",&mod[i],&a[i]); 64 printf("%lld",CRT(a,mod,m)); 65 } 66 int k(gg()); 67 int main(){;}
标签:任务 ble 代码 整数 tput 接下来 思想 inline stdin
原文地址:http://www.cnblogs.com/hzoi-mafia/p/7275944.html