hdu 3579 Hello Kiki

来源:互联网 发布:java构造函数的特点 编辑:程序博客网 时间:2024/05/19 19:59

题目:点击打开题目

#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;const int MAXN = 10;LL Gcd(LL a, LL b){    return b == 0 ? a : Gcd(b, a%b);}void Ex_Gcd(LL a, LL b, LL &d, LL &x, LL&y){    if(b == 0)    {        x = 1;        y = 0;        d = a;        return ;    }    else    {        Ex_Gcd(b, a%b, d, x, y);        LL t = x;        x = y;        y = t - (a/b) * y;    }}int main(){    int n,i;    LL lcm, a, b, c, x0, y0, d;    LL aa[MAXN], bb[MAXN];    int kcase = 1;    int T;    cin>>T;    while(T--)    {        lcm = 1;        memset(aa, 0, sizeof(aa));        memset(bb, 0, sizeof(bb));        cin>>n;        bool flag = true;        for(i = 0; i < n; ++i)        {            cin>>aa[i];            lcm = lcm / Gcd(lcm, aa[i]) * aa[i];        }        for(i = 0; i < n; ++i)            cin>>bb[i];        cout<<"Case "<<kcase++<<": ";        for(i = 1; i < n; ++i)        {            a = aa[0];            b = aa[i];            c = bb[i] - bb[0];            Ex_Gcd(a, b, d, x0, y0);            if(c % d != 0)            {                flag = false;                break;            }            LL t = b / d;            x0 = x0 * (c/d);            x0 = (x0 % t + t) % t;            bb[0] = aa[0] * x0 + bb[0];            aa[0] = aa[0] * aa[i] / d;        }        if(!flag)        {            cout<<"-1"<<endl;            continue;        }        if(bb[0] != 0)        {            cout<<bb[0]<<endl;            continue;        }        cout<<lcm<<endl;    }    return 0;}


0 0