题意:三个高峰周期分别为23,28,33,给你一个d这一年过了几天,p,e,i表示前一次三个高峰距d过了多少天,求三个高峰在d之后的第几天出现
思路:中国剩余定理 :http://blog.csdn.net/leonharetd/article/details/13167615
根据题意列同与方程
d = p mod 23
d = e mod 28
d = i mod 33
解同余方程得出最小的解x,题目要求d之后的天数所以求通解直到大于d,计算x-d就是结果
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long int64;
int64 Exgcd(int64 a,int64 b,int64 &x,int64 &y)
{
if(b == 0)
{
x = 1,y = 0;
return a;
}
else
{
int64 r = Exgcd(b,a%b,x,y);
int64 temp = x;
x = y,y = temp - a/b*y;
return r;
}
}
int main()
{
int64 d,p,e,i,cas = 1;
while(scanf("%I64d%I64d%I64d%I64d",&p,&e,&i,&d) != EOF)
{
if(d == -1 && p == -1 && e == -1 && i == -1)
break;
int64 M = 23*28*33;
int64 A[5] = {23,28,33};
int64 C[5] = {p,e,i};
int64 a,b,r,x,y,ans = 0;
for(int i = 0; i < 3; i++)
{
int64 Mi = M / A[i];
r = Exgcd(Mi,A[i],x,y);
b = A[i] / r;
x = x / r * C[i];
x = ( x % b + b ) % b;
ans = (ans + x*Mi) % M;
}
while(ans <= d)
ans += M;
printf("Case %I64d: the next triple peak occurs in %I64d days.\n",cas++,ans-d);
}
return 0;
}