题意:求数n,使(n+d)%23==p,(n+d)%28==e,(n+d)%33=i;
转载请注明出处:http://www.cnblogs.com/dashuzhilin/;
思路:中国剩余定理。利用同余的加性,将(n+d)拆成三个数a,b,c,
使a%23==p,a%28==0,a%33==0;
使b%23==0,b%28==e,b%33==0;
使c%23==0,c%28==0,c%33==i;
则(n+d)==(a+b+c)+lcm(23,28,33)*t;
那么,利用同余的乘性,我们可以做一下优化,初始时,令p,e,i均为1,即:
使a%23==1,a%28==0,a%33==0;a为28、33的倍数;
使b%23==0,b%28==1,b%33==0;b为23、33的倍数;
使c%23==0,c%28==0,c%33==1;c为23、28的倍数;
通过预处理,枚举得到a,b,c的值,则(n+d)=a*p+b*e+c*i+lcm(23,33,28)*t可以得到所求结果;
预处理代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int a,b,c,i,j,k; for(i=1;i<=500000;i++) { if(28*33*i%23==1) { printf("%d ",28*33*i);break; } } for(i=1;i<=500000;i++) { if(23*33*i%28==1) { printf("%d ",23*33*i);break; } } for(i=1;i<=500000;i++) { if(23*28*i%33==1) { printf("%d ",23*28*i);break; } } }
源代码:
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main() { int a,b,c,i,j=0,k,x,y,z,d; while(scanf("%d%d%d%d",&x,&y,&z,&d)!=EOF) { if(x==-1&&y==-1&&z==-1&&d==-1) break; k=5544*x+14421*y+1288*z-d; k=k%21252; if(k<=0) k=21252-d; printf("Case %d: the next triple peak occurs in %d days. ",++j,k); } return 0; }