nyoj 151 Biorhythms

版权声明:本文为博主原创文章,转载需注明出处。 https://blog.csdn.net/Hearthougan/article/details/24812395

题目来源:http://acm.nyist.net/JudgeOnline/problem.php?pid=151

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

using namespace std;


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

int China(int &M, int r, int a[], int m[], int ans)
{
    int i, x0, y0;
    for(i = 0; i < r; ++i)
        M *= m[i];
    ans = 0;
    for(i = 0; i < r; ++i)
    {
        int mi = M / m[i];
        ExGcd(mi, m[i], x0, y0);
        ans = ans + mi*a[i]*x0;
    }
    return (ans%M+M)%M;
}

int main()
{
    int a[3], b[3], p, e, i, d, ans, M;
    while(~scanf("%d %d %d %d", &p, &e, &i, &d))
    {
        M = 1;
        if(p == -1 && e == -1 && i == -1 && d == -1)
            break ;
        memset(a, 0, sizeof(a));
        memset(b, 0, sizeof(b));
        a[0] = p, a[1] = e, a[2] = i;
        b[0] = 23, b[1] = 28, b[2] = 33;
        ans = China(M, 3, a, b, 0);
        ans -= d;
        if(ans <= 0)
            ans += M;
        printf("%d\n", ans);
    }
    return 0;
}


阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页