Simple physiological Cycle Simulation
Perform the remainder operation when the value exceeds 23*28*33 (21252.
#include<stdio.h>int main(){ //freopen("in.txt","r",stdin); int a,b,c,d,s,m=1; while(scanf("%d %d %d %d",&a,&b,&c,&d),(a!=-1||b!=-1||c!=-1||d!=-1)) { s=a; if(b>s) s=b; if(c>s) s=c; do { s++; }while (((s+d-a)%23!=0)|| ((s+d-b))%28!=0 ||((s+d-c)%33!=0) ); if(s>21252) s=s%21252; printf("Case %d: the next triple peak occurs in %d days.\n",m,s); m++; } return 0;}
China Remainder Theorem
If a certain number of X is D1 ,,... The remainder of the DN division is R1, R2 ,... , RN, can be expressed as the following formula:
X = r1r1 + r2r2 +... + Rnrn + Rd
R1 is D2, D3 ,... , DN public multiple, and division by D1, the remainder is 1;
R1, R2... ,
RN is D1, D2 ,... , The dn-1 of the public multiple, And the DN division, the remainder is 1;
D is D1, D2 ,... ;
R is an arbitrary integer and can be determined based on actual needs;
And D1 ,,... , Must be mutually qualitative, to ensure that each RI (I = 1, 2 ,..., N) can be obtained.
In fact, poj1006 is a typical application of China's surplus theorem.
Obtain the constant R1 = 5544, R2 = 14421, R3 = 1288, D = 21252 according to the preceding conditions.
The first three values in the data given each time are equal to the remainder of the above.
Obviously:
Res = R1 * Physical + R2 * emotion + R3 * intelligence + RD-givendate
And 0 = <res <= d
#include <stdio.h>int main(){ int R1 = 5544, R2 = 14421, R3 = 1288, R = 21252; int ph, em, in, day; int res,i; for(i=1; 1; i++){ scanf("%d%d%d%d", &ph, &em, &in, &day); if(ph==-1)break; res = (R1 * ph + R2 * em + R3 * in - day) % R; res = (res + R - 1) % R + 1; //此步处理了负数和零的两种边界情况 printf("Case %d: the next triple peak occurs in %d days.\n", i, res); } return 0;}
Submission result:
Simulate 782 Ms
Chinese Remainder Theorem 32 Ms
It's too high-end !!!!!!
[Chinese Remainder Theorem] poj 1006