poj 1006 Biorhythms 中国剩余定理

题目地址: http://poj.org/problem?id=1006

中国剩余定理的应用

#include<iostream>
using namespace std;
typedef long long inta;

void extend_gcd(inta a,inta b,inta &x,inta &y)
{
  if(b==0)
   {
      x=1;
      y=0;
   }
   else
   {
      extend_gcd(b,a%b,x,y);
      int temp=x;
      x=y;
      y=temp-a/b*y;
   }
}
int main()
{
  int tag=1;
  inta a,b,c,d;
  int  mod=21252;
  inta  M[3];
   M[0]=924;
   M[1]=759;
   M[2]=644;
   int m[3];
   m[0]=23;
   m[1]=28;
   m[2]=33;
   int r[3];

  while(cin>>a>>b>>c>>d)
   {
      if(a==-1&&b==-1&&c==-1&&d==-1)  break;
      cout<<"Case "<<tag<<": the next triple peak occurs in ";
       tag++;
      inta ans=0;
      inta x,y;
      r[0]=a;
      r[1]=b;
      r[2]=c;
      for(int i=0;i<3;i++)
         {
             extend_gcd(M[i],m[i],x,y);
             ans+=(x*M[i]*r[i])%mod;
         }
      ans=(ans%mod+mod)%mod;
      while(ans<=d)
      ans+=mod;

      cout<<ans-d<<" days."<<endl;
   }
}


posted @ 2013-07-10 04:18  814jingqi  阅读(104)  评论(0编辑  收藏  举报