HDU1370 Biorhythms (中国剩余定理模板)

题目:
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值