中国剩余定理……、
链接http://acm.hdu.edu.cn/showproblem.php?pid=1370
1 /************************************************************************* 2 > File Name: xh.cpp 3 > Author: XINHUA 4 > Mail: 525799145@qq.com 5 > Created Time: 2013/7/11 星期四 17:46:00 新华 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<stdio.h> 10 #include<algorithm> 11 #include<iomanip> 12 using namespace std; 13 int extend_gcd(int a,int b,int &x,int &y) 14 { 15 int m; 16 if(b==0) 17 { 18 x=1;y=0; 19 return a; 20 } 21 else 22 { 23 m=extend_gcd(b,a%b,x,y); 24 int c=x; 25 x=y; 26 y=c-a/b*(y); 27 } 28 return m; 29 } 30 int mod(int a,int m) 31 { 32 int x,y,gcd; 33 gcd=extend_gcd(a,m,x,y); 34 if(gcd==1) 35 { 36 if(x<0) 37 x=(x%m+m)%m; 38 return x; 39 } 40 else return 0; 41 } 42 int china(int m0[],int b[],int d) 43 { 44 int x,y,n,m=1,a=-d; 45 int i; 46 for(i=0;i<3;i++) m*=m0[i]; 47 for(i=0;i<3;i++) 48 { 49 n=m/m0[i]; 50 extend_gcd(n,m0[i],x,y); 51 a=a+n*b[i]*x; 52 } 53 return a%m; 54 } 55 int main() 56 { 57 int p,e,i,d,k,sum,l=0,m[3]={ 58 23,28,33 59 },b[3]; 60 char c; 61 cin>>c; 62 while(cin>>p>>e>>i>>d) 63 { 64 if(p==-1&&e==-1&&i==-1&&d==-1) 65 break; 66 b[0]=p;b[1]=e;b[2]=i; 67 sum=china(m,b,d); 68 if(sum<=0) sum+=21252; 69 printf("Case %d: the next triple peak occurs in %d days. ",++l,sum); 70 } 71 return 0; 72 }