题目:
http://acm.hdu.edu.cn/showproblem.php?pid=1370
代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int tmax=5;
ll M,a[tmax],m[tmax];
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
{
x=1;
y=0;
return;
}
exgcd(b,a%b,y,x);
y-=x*(a/b);
return;
}
ll china(int n,ll *a,ll *m)
{
ll d,x=0,Mi,inv,y;
M=1;
for(int i=1;i<=n;i++) M*=m[i];
for(int i=1;i<=n;i++)
{
Mi=M/m[i];
exgcd(Mi,m[i],inv,y); //这里不能用快速幂求逆元,因为m[i]不一定为素数
x=(x+a[i]*Mi%M*inv%M)%M; //x=(x+a[i]*Mi*inv(Mi,m[i]-2))%M;
}
return (x%M+M)%M;
}
int main()
{
int p,e,i,day,T,kase;
cin>>T;
while(T--)
{
kase=0;
while(1)
{
kase++;
scanf("%d%d%d",&p,&e,&i);
if(p==-1) break;
a[1]=p;
a[2]=e;
a[3]=i;
m[1]=23;
m[2]=28;
m[3]=33;
scanf("%d",&day);
ll ans=china(3,a,m);
if(ans<=1ll*day) ans+=M;
printf("Case %d: the next triple peak occurs in %I64d days.\n",kase,ans-day);
}
}
return 0;
}