Poj 1006

中国剩余定理简单应用

#include <iostream>

#include <stdio.h>

using namespace std;

 

int b[3],w[3];//a=b[i](%w[i]);

 

int exgcd(int a,int b,int &x,int &y)

{

if(0==b){x=1;y=0;return a;}

int d=exgcd(b,a%b,x,y);

int t=x;x=y;y=t-a/b*y;

return d;

}

 

int china(int b[],int w[],int k)//

{

int i,d,x,y,m,a=0,n=1;

for(i=0;i<k;i++) n*=w[i];

for(i=0;i<k;i++)

{

m=n/w[i];

d=exgcd(w[i],m,x,y);

a=(a+y*m*b[i])%n;

}

if(a>0) return a;

else return a+n;

}

 

int main()

{

int k,times;

w[0]=23;

w[1]=28;

w[2]=33;

freopen("in.txt","r",stdin);

int tt=1;

while(scanf("%d%d%d%d",&b[0],&b[1],&b[2],&times)!=EOF)

{

if(b[0]==-1 && b[1]==-1 && b[2]==-1 && times==-1)

break;

printf("Case %d: the next triple peak

occurs in %d days.\n",tt++,china(b,w,3)-times>0?china(b,w,3)-times:21252+china(b,w,3)-times);

}

return 0;

}

posted on 2012-03-15 20:41  Inpeace7  阅读(121)  评论(0编辑  收藏  举报

导航