poj 1006 Biorhythms

题目:Biorhythms

思路: 扩展欧几里得 

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <map>
using namespace std;
int w[4]={0,23,28,33},a[4];
long long exgcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    else
    {
        long long ans=exgcd(b,a%b,x,y);
        long long t=x;
        x=y;
        y=t-a/b*y;
        return ans;
    }
}
int CRT(int r)
{
    long long M=1;
    long long i,d,x0,y0,ans=0;
    for(i=1;i<=r;i++)
        M*=w[i];//好像很容易溢出的样子,CRT弱爆了
    for(i=1;i<=r;i++)
    {
        d=M/w[i];
        exgcd(d,w[i],x0,y0);
        ans=(ans+d*x0*a[i])%M;
    }
    while(ans<= 0)
       ans+=M;
    return ans;
}
int main()
{
    int ca=0;
    int d;
    while(scanf("%d%d%d%d",&a[1],&a[2],&a[3],&d)!=EOF)
    {
        if(a[1]==-1&&a[2]==-1&&a[3]==-1&&d==-1)
            break;
        int M=1;
        for(int i=1;i<=3;i++)
            M*=w[i];
        int ans=CRT(3);
        ans-=d;
        while(ans<=0)
            ans+=M;
        printf("Case %d: the next triple peak occurs in %d days.\n",++ca,ans);
    }
    return 0;
}
View Code

 

posted @ 2013-07-16 15:49  over_flow  阅读(139)  评论(0编辑  收藏  举报