HDU 1006 Biorhythms (中国剩余定理)

题解思路:

比较裸的中国剩余定理

https://blog.csdn.net/niiick/article/details/80229217

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<algorithm>
#include<map>

#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define int long long

using namespace std;

const int maxn=1e5+10;

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

#undef int
int main(){
#define int long long
    int st,a[5],b[5]={0,23,28,33},m,mub=0;
    while(cin>>a[1]>>a[2]>>a[3]>>st)
    {
        m=1;
        if(st+a[1]+a[2]+a[3]==-4) break;
        for(int i=1;i<=3;i++)
        {
            //a[i]%=b[i];
            m*=b[i];
        }
        //cout<<m<<endl;
        int x,y,ans=0;
        for(int i=1;i<=3;i++)
        {
            ex_gcd(m/b[i],b[i],x,y);
           // int t=b[i]/g;
            //x=(x*(a[i]/g))%b[i];
            x=(x%b[i]+b[i])%b[i];
            //cout<<x<<endl;
            ans=(ans+x*(m/b[i])*a[i])%m;
        }
        ans-=st;
        ans=((ans%m+m)%m)%m;
        if(!ans) ans+=m;
        printf("Case %lld: the next triple peak occurs in %lld days.\n",++mub,ans);
    }
    return 0;
}

 

posted @ 2019-02-21 16:40  Minun  阅读(139)  评论(0编辑  收藏  举报