HDU 1370 Biorhythms

题目大意

3 个循环周期,周期天数分别为 23 , 28 , 33 。对于某一年,已知某年这 3 个周期的某一峰值分别是当年的第 p , e , i 天, 问从第 d 天开始到最近一个满足 3 个周期都达到峰值的日期还有多少天。


中国剩余定理入门题。

设答案为 x d ,有:

{ x p ( m o d   23 ) x e ( m o d   28 ) x i ( m o d   33 )

将每个式子写成 x a i   ( m o d   m i ) 的形式。

#include <cstdio>

int m[5] = {0, 23, 28, 33}, a[5], n = 3;

void exgcd(int a, int b, int &x, int &y) {
    if (b == 0) { x = 1, y = 0; return; }
    exgcd(b, a % b, y, x); y -= a / b * x;
}
int crt() {
    int t = 1, x, y, res = 0;
    for (int i = 1; i <= n; ++i) t *= m[i];
    for (int i = 1; i <= n; ++i) { //找到一个数是其他模数的倍数,且在模m[i]的意义下等于1
        exgcd(t / m[i], m[i], x, y); //x * t / m[i] % m[i] = 1
        res = (res + x * t / m[i] * a[i]) % t;
    }
    return (res + t) % t;
}

int main() {
    int cas, d; scanf("%d", &cas);
    while (~scanf("%d%d%d%d", &a[1], &a[2], &a[3], &d)) {
        if (a[1] == -1) break;
        a[1] %= 23, a[2] %= 28, a[3] %= 33;
        int ans = crt() - d;
        if (ans <= 0) ans += 21252;
        printf("Case %d: the next triple peak occurs in %d days.\n", cas++, ans);
    }
    return 0;
}

u p d a t e : 自从学会了 e x c r t ,我决定要开心地忘掉难记的 c r t ٩ ( > < ) ۶

猜你喜欢

转载自blog.csdn.net/Milkyyyyy/article/details/81734863
今日推荐