poj 1006 Biorhythms

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int m[4], a[4];
int M;

void Ex_Gcd(int a, int b, int &d, int &x, int &y)
{
    if(b == 0)
    {
        x = 1, y = 0;
        d = a;
        return ;
    }
    else
    {
        Ex_Gcd(b, a%b, d, x, y);
        int temp = x;
        x = y;
        y = temp - (a/b) * y;
    }
}

int China( int r )
{
    int i;
    M = 1;
    int Mi, x0, y0, d, ans = 0;
    for(i = 1; i <= r; ++i)
        M *= m[i];
    for(i = 1; i <= r; ++i)
    {
        Mi = M / m[i];
        Ex_Gcd(Mi, m[i], d, x0, y0);
        ans = (ans + Mi * x0 * a[i]) % M;
    }
    if(ans < 0)
        ans += M;
    return ans;
}


int main()
{
    int kcase = 1;
    int p, e, i, d;
    while(~scanf("%d %d %d %d", &p, &e, & i, &d))
    {
        if(p == -1 && e == -1 && i == -1 && d == -1)
            break;
        a[1] = p, a[2] = e, a[3] = i;
        m[1] = 23, m[2] = 28, m[3] = 33;
        int ans = China( 3 );
        while(ans <= d)
            ans += M;
        cout<<"Case "<<kcase++<<": the next triple peak occurs in "<<ans-d<<" days."<<endl;
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值