Test instructions: number n, is (n+d)%23==p, (N+d)%28==e, (n+d)%33=i;
Reprint please indicate source: http://www.cnblogs.com/dashuzhilin/;
Idea: Chinese remainder theorem. Using the additive of congruence, the (n+d) is split into three number a,b,c,
Make a%23==p,a%28==0,a%33==0;
Make b%23==0,b%28==e,b%33==0;
Make c%23==0,c%28==0,c%33==i;
Then (n+d) = = (A+b+c) +LCM (23,28,33) *t;
So, we can do the optimization, initially, the p,e,i are 1, namely:
Make the A%23==1,a%28==0,a%33==0;a a multiple of 28 and 33;
Make the B%23==0,b%28==1,b%33==0;b a multiple of 23 and 33;
Make the C%23==0,c%28==0,c%33==1;c a multiple of 23 and 28;
By preprocessing, the enumeration gets the value of A,b,c, then (n+d) =A*P+B*E+C*I+LCM (23,33,28) *t can get the result;
Preprocessing code:
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intMain () {inta,b,c,i,j,k; for(i=1; i<=500000; i++) { if( -* -*i% at==1) {printf ("%d", -* -*i); Break; } } for(i=1; i<=500000; i++) { if( at* -*i% -==1) {printf ("%d", at* -*i); Break; } } for(i=1; i<=500000; i++) { if( at* -*i% -==1) {printf ("%d\n", at* -*i); Break; } }}
Source:
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intMain () {inta,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.\n",++j,k); } return 0;}
POJ 1006 biorhythms Chinese remainder theorem