现在的位置: 首页 > 综合 > 正文

HDU 3579 Hello Kiki 中国剩余定理 不互质情况

2018年05月02日 ⁄ 综合 ⁄ 共 2341字 ⁄ 字号 评论关闭

Hello Kiki

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2233    Accepted Submission(s): 808

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
 

/*
HDOJ 3579 中国剩余定理 不互质情况 

扩展欧几里德:
ax1+by1=gcd(a,b);同理:
bx2+(a mod b)y2=gcd(b,a mod b); 
==》x1=y2; y1=x2-(a/b)*y2;
 
两个方程合并的一种方法:
x = c1 (mod b1)
x = c2(mod b2) 
此时b1,b2不必互质的。
显然可以得到x=k1*b1+c1  x=k2*b2+c2,
两个方程合并一下就可以得到:
k1*b1-k2*b2=c2-c1
  
根据一元线性同余方程的方法: 
这样可以设g=gcd(b1,b2),于是就有
b1/g*k1- b2/g*k2=(c2-c1)/g,

方程又回到上面的那个。 
:k1=K(mod(b2/g)),
最后得到x=K*b1+c1 (mod(b1*b2/g))。

对于题目所给正整数的要求,只有一种反例,就是结果输出为0的情况,
这个可以特殊考虑,只需要考虑所有数的最小公倍数即可。
*/
#include <iostream>   
#include <cstdio>   
#include <cstdlib>   
#include <cstring>   
using namespace std; 
 
int x,y,t;  
 
int egcd(int a, int b)//扩展欧几里德 求出ax+by=gcd(a,b) 
{  
    if (b==0)     
    {  
        x=1;        
        y=0;     
        return a;     
    }  
    else   
    {  
        int e=egcd(b,a%b);   
        t=x;   
        x=y;  
        y=t-a/b*y;     
        return e;     
    }  
}  

int gcd(int x,int y)  
{  
    if(!x || !y)  
        return x > y ? x : y;  
    for (int t; t=x%y;x=y,y=t);  
    return y;  
} 
 
int mm[10],rr[10];  

int main()   
{  
	int T,Case,N,i,ans;
    int m1,m2,r1,r2,d,c,t;
    bool flag;  
	   
    scanf("%d",&T);  
    for(Case=1;Case<=T;Case++)      
    {  
        scanf("%d",&N);  
        flag=0;            
        for(i=0;i<N;i++)   
            scanf ("%d",&mm[i]);  
        for(i=0;i<N;i++)  
            scanf ("%d",&rr[i]);  
    
        m1=mm[0];  
        r1=rr[0];  
        for(i=1;i<N;i++)      
        {  
            m2=mm[i];  
            r2=rr[i];     
            if(flag)   
                continue;     
            d=egcd(m1, m2);     
            c=r2-r1;   
            if(c%d)        
            {         
                flag=1;      
                continue;     
            }  
            t=m2/d;      
            x=(c/d*x%t+t)%t;     
            r1=m1*x+r1;      
            m1=m1*m2/d;      
        } 
		 
        if(flag)  
            printf ("Case %d: -1\n",Case);  
        else  
        {  
            if (r1==0&&N>1)  
            {  
                r1=mm[0];  
                ans=1;  
                for(i=1;i<N;i++)  
                    r1=gcd(mm[i],r1);  
                for(i=0;i<N;i++)  
                    ans*=mm[i];  
                r1=ans/r1;  
            }  
            if(r1==0&&N==1)  
                r1=mm[0];  
            printf ("Case %d: %d\n",Case,r1);  
        }  
    }  
    return 0;  
}  

抱歉!评论已关闭.