uva 756 - Biorhythms(中国剩余定理)

942 篇文章 2 订阅
202 篇文章 0 订阅

题目链接:uva 756 - Biorhythms

题目大意:三个周期,23,28,33,输入为分别为在新一年中(三个周期均从0开始),出现周期中峰值的一天,以及当前的日子,问说需要经过多少天,才能使得三个峰值的在同一天。

解题思路:裸的中国剩余定理。

#include <cstdio>
#include <cstring>

typedef long long ll;
const int maxn = 5;
const ll m[maxn] = {23,28,33};

ll M, t[maxn], D;

bool init () {
    bool flag = false;
    for (int i = 0; i < 3; i++) {
        scanf("%lld", &t[i]);
        if (t[i] != -1)
            flag = true;
    }

    scanf("%lld", &D);
    if (D != -1)
        flag = true;

    return flag;
}

void gcd (ll a, ll b, ll& d, ll& x, ll& y) {
    if (b == 0) {
        d = a;
        x = 1;
        y = 0;
    } else {
        gcd(b, a%b, d, y, x);
        y -= (a/b) * x;
    }
}

ll china (ll* a, int n) {
    M = 1;
    for (int i = 0; i < n; i++)
        M *= m[i];

    ll ans = 0;
    for (int i = 0; i < n; i++) {
        ll w = M/m[i], d, x, y;
        gcd(m[i], w, d, x, y);

        ans = (ans + y*w*a[i]) % M;
    }
    ans = (ans + M) % M;
    return ans;
}

int main () {
    int cas = 1;
    while (init()) {
        ll ans = china(t,3);
        ll t = ans;
        while (ans < D || ans == 0)
            ans += M;
        printf("Case %d: the next triple peak occurs in %lld days.\n", cas++, ans - D);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值