poj1006 中国剩余定理(互质)

/*
Time:2019.12.15
Author: Goven
type:中国剩余定理 
ref:
*/
#include

using namespace std;

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

int main()
{
    int p, e, i, d;
    int cnt = 0;
    while (cin >> p >> e >> i >> d) {
        cnt++;
        if (p == -1 && e == -1 && i == -1 && d == -1) break;
        int M = 23 * 28 * 33;
        int x, y, td;
        ext_gcd(28 * 33, 23, x, y, td);//求解逆元 
        x = (x % 23 + 23) % 23;
        int tp = x * (M / 23) % M * p % M;
        
        ext_gcd(23 * 33, 28, x, y, td);
        x = (x % 28 + 28) % 28;
        tp = (tp + x * (M / 28) % M * e % M) % M;

        ext_gcd(28 * 23, 33, x, y, td);
        x = (x % 33 + 33) % 33;
        tp = (tp + x * (M / 33) % M * i % M) % M;

        tp = ((tp - d) % M + M) % M;
        if (!tp) tp = M;
        printf("Case %d: the next triple peak occurs in %d days.\n", cnt, tp);
    }
    return 0;
}

你可能感兴趣的:(poj1006 中国剩余定理(互质))