hdu 3579 Hello Kiki 不互质的中国剩余定理

Hello Kiki

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)


Problem Description
One day I was shopping in the supermarket. There was a cashier counting coins seriously when a little kid running and singing "门前大桥下游过一群鸭,快来快来 数一数,二四六七八". And then the cashier put the counted coins back morosely and count again...
Hello Kiki is such a lovely girl that she loves doing counting in a different way. For example, when she is counting X coins, she count them N times. Each time she divide the coins into several same sized groups and write down the group size Mi and the number of the remaining coins Ai on her note.
One day Kiki's father found her note and he wanted to know how much coins Kiki was counting.
 
Input
The first line is T indicating the number of test cases.
Each case contains N on the first line, Mi(1 <= i <= N) on the second line, and corresponding Ai(1 <= i <= N) on the third line.
All numbers in the input and output are integers.
1 <= T <= 100, 1 <= N <= 6, 1 <= Mi <= 50, 0 <= Ai < Mi
 
Output
For each case output the least positive integer X which Kiki was counting in the sample output format. If there is no solution then output -1.
 
Sample Input
2 2 14 57 5 56 5 19 54 40 24 80 11 2 36 20 76
 
Sample Output
Case 1: 341 Case 2: 5996
 
Author
digiter (Special Thanks echo)
 
Source
思路,不互质的中国剩余定理,无解输出-1,有解输出一个最小的正整数(正正正。。。重要的事说三遍)也就是说这题有个答案为0的时候需要输出最小公倍数;
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
//#pragma comment(linker, "/STACK:102400000,102400000")
int scan()
{
    int res = 0 , ch ;
    while( !( ( ch = getchar() ) >= '0' && ch <= '9' ) )
    {
        if( ch == EOF ) return 1 << 30 ;
    }
    res = ch - '0' ;
    while( ( ch = getchar() ) >= '0' && ch <= '9' )
        res = res * 10 + ( ch - '0' ) ;
    return res ;
}
ll a[100010];
ll b[100010];
ll gcd(ll x,ll y)
{
    if(x%y==0)
    return y;
    else
    return gcd(y,x%y);
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    exgcd(b, a % b, x, y);
    ll tmp = x;
    x = y;
    y = tmp - (a / b) * y;
}
int main()
{
    ll x,y,z,i,t;
    ll flag=1;
    scanf("%lld",&x);
    while(x--)
    {
        scanf("%lld",&z);
        for(i=0;i<z;i++)
        scanf("%lld",&b[i]);
        for(i=0;i<z;i++)
        scanf("%lld",&a[i]);
        ll a1=a[0],b1=b[0];
        ll jie=1;
        for(i=1;i<z;i++)
        {
            ll a2=a[i],b2=b[i];
            ll xx,yy;
            ll gys=gcd(b1,b2);
            if((a2-a1)%gys)
            {
                jie=0;
                break;
            }
            exgcd(b1,b2,xx,yy);
            xx=(xx*(a2-a1))/gys;
            ll gbs=b1*b2/gys;
            a1=(((xx*b1+a1)%gbs)+gbs)%gbs;
            b1=gbs;
        }
        printf("Case %lld: ",flag++);
        if(!jie)
        printf("-1
");
        else if(a1!=0)
        printf("%lld
",a1);
        else
        printf("%lld
",b1);
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/jhz033/p/5417816.html