1 /* 2 中國剩余定理可以描述為: 3 若某數x分別被d1、、…、dn除得的余數為r1、r2、…、rn,則可表示為下式: 4 x=R1r1+R2r2+…+Rnrn+RD 5 其中R1是d2、d3、…、dn的公倍數,而且被d1除,余數為1;(稱為R1相對于d1的數論倒數) 6 R1 、 7 R2 、 8 … 、 9 Rn是d1、d2、…、dn-1的公倍數,而且被dn除,余數為1; 10 D是d1、d2、…、的最小公倍數; 11 R是任意整數(代表倍數),可根據實際需要決定; 12 且d1、、…、必須互質,以保證每個Ri(i=1,2,…,n)都能求得. 13 */ 14 #include<iostream> 15 using namespace std; 16 int main(){ 17 int a, b, c, d; 18 int cnt=0; 19 int x23=5544, x13=14421, x12=1288, x=21252; 20 //x23為b,c的公倍數, 且x23%==1 x23為a,c的公倍數, 且x23%==1 x13為a,b的公倍數, 且x12%c==1 21 //a, b, c 為余數 22 while(cin>>a>>b>>c>>d && a!=-1){ 23 int res=(a*x23 + b*x13 + c*x12) % x; 24 res-=d; 25 if(res<=0) 26 res=(res+x-1)%x+1; 27 cout<<"Case "<<++cnt<<": the next triple peak occurs in "<<res<<" days."<<endl; 28 } 29 return 0; 30 }
?