hdu 3579 Hello Kiki

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=3579

要点; 题目要求正整数,一旦r1=0;应该输出最小公倍数

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

void extend_gcd(inta a,inta b,inta &x,inta &y,inta &gcd)
{
    if(b==0)
    {
       x=1;
       y=0;
       gcd=a;
    }
    else
    {
      extend_gcd(b,a%b,x,y,gcd);
      inta temp=x;
      x=y;
      y=temp-a/b*y;

    }
}
int main()
{

   int size=0;
   cin>>size;
   for(int i=0;i<size;i++)
 {
    int n;
    cin>>n;
    int *a=new int[n];
    int *r=new int[n];

     for(int i=0;i<n;i++)
        cin>>a[i];
     for(int i=0;i<n;i++)
        cin>>r[i];

   inta a1,r1,a2,r2,x,y;
    a1=a[0];
    r1=r[0];
    int bad=0;
   for(int i=1;i<n;i++)
    {
       a2=a[i];
       r2=r[i];
       inta a=a1;
       inta b=a2;
       inta c=r2-r1;
       inta q=1;
       extend_gcd(a,b,x,y,q);
       if(c%q!=0)
       {
          bad=1;
       }

       else
       {
          inta t=b/q;
          x=(c/q*x%t+t)%t;
          r1=a1*x+r1;
          a1=a1/q*a2;
          r1=r1%a1;
       }
    }
     cout<<"Case "<<i+1<<": ";
      if(bad==0)
      {
      if(r1>0)
      cout<<r1<<endl;
      else
      cout<<a1<<endl;
      }
      else cout<<-1<<endl;

   }

}


posted @ 2013-07-10 02:33  814jingqi  阅读(107)  评论(0编辑  收藏  举报