题目:http://poj.org/problem?id=1006
中国剩余定理:x= m/mj + bj + aj 讲解:http://www.cnblogs.com/MashiroSky/p/5918158.html
逆元: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<iostream>
#include<cstdio>
using namespace std;
int a1,a2,a3,m1=,m2=,m3=,mm1=,mm2=,mm3=,m=,st;
int x,y,k,t;
void exgcd(int a,int b)
{
if(!b)
{
x=;
y=;
return;
}
exgcd(b,a%b);
int tem=y;
y=x-a/b*y;
x=tem;
}
int main()
{
while()
{
k=;t++;
scanf("%d%d%d%d",&a1,&a2,&a3,&st);
if(a1==-&&a2==-&&a3==-&&st==-)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 ;
}