题目:
中国剩余定理:x= m/mj + bj + aj 讲解:
逆元:m/mj * bj -mj *y=1——m/mj * bj = 1 mod mj
拓展欧几里得:p*a+q*b=gcd(a,b)——p'=q ; q'=p - a/b * q
#include#include using namespace std;int a1,a2,a3,m1=23,m2=28,m3=33,mm1=924,mm2=759,mm3=644,m=21252,st;int x,y,k,t;void exgcd(int a,int b){ if(!b) { x=1; y=0; return; } exgcd(b,a%b); int tem=y; y=x-a/b*y; x=tem;}int main(){ while(1) { k=0;t++; scanf("%d%d%d%d",&a1,&a2,&a3,&st); if(a1==-1&&a2==-1&&a3==-1&&st==-1)break; a1%=m1;a2%=m2;a3%=m3; exgcd(mm1,m1); k+=mm1*a1*x;k%=m; exgcd(mm2,m2); k+=mm2*a2*x;k%=m; exgcd(mm3,m3); k+=mm3*a3*x;k%=m; while(k<=st)k+=m; k-=st; printf("Case %d: the next triple peak occurs in %d days.\n",t,k); } return 0;}