首页 > ACM题库 > HDU-杭电 > HDU 3579-Hello Kiki-数论-[解题报告]HOJ
2014
11-27

HDU 3579-Hello Kiki-数论-[解题报告]HOJ

Hello Kiki

问题描述 :

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.

输入:

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

输出:

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

样例输入:

2
2
14 57
5 56
5
19 54 40 24 80
11 2 36 20 76

样例输出:

Case 1: 341
Case 2: 5996

 

hdu 3579 Hello Kiki 中国剩余定理(不互质的情况)
对互质的情况,处理起来比较方便,可以直接套模板
本题给出不互质的模线性方程组,求出满足方程的最小正整数解
方案:对于不互质的模线性方程组,可以进行方程组合并,求出合并后的方程的解,这样就可以很快地推出方程的最终解。
两个方程合并的一种方法:
x = c1 (mod b1)
x = c2(mod b2) 
此时b1,b2不必互质的。
显然可以得到x = k1 * b1 + c1   x = k2* b2 + c2,
两个方程合并一下就可以得到:k1 * b1 = c2 – c1 (mod b2),
这样可以设g=gcd(b1,b2),于是就有b1/g*k1-b2/g*k2=(c2-c1)/g,
显然判断(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;  
__int64 x, y, t;  
__int64 egcd(__int64 a, __int64 b)   
{  
    if (b==0)     
    {  
        x=1;        
        y=0;     
        return a;     
    }  
    else   
    {  
        __int64 e=egcd(b,a % b);   
        t=x;   
        x=y;  
        y=t-a/b*y;     
        return e;     
    }  
}  
__int64 gcd(__int64 x, __int64 y)  
{  
    if (!x || !y)  
        return x > y ? x : y;  
    for (__int64 t; t = x % y; x = y, y = t);  
    return y;  
}  
__int64 mm[10],rr[10];  
int main()   
{  
	int T,Case,N;
    __int64 m1,m2,r1,r2,d,c,t;  
    bool flag;     
    scanf ("%d",&T);  
    for(Case = 1; Case <= T; Case++)      
    {  
        scanf ("%d",&N);  
        flag=0;            
        for (__int64 i=0;i<N;i++)  
        {  
            scanf ("%I64d",&mm[i]);  
        }  
        for (__int64 i=0;i<N;i++)  
        {  
            scanf ("%I64d",&rr[i]);  
        }  
        m1=mm[0];  
        r1=rr[0];  
  
        for (__int64 i = 0; i < N - 1; i++)      
        {  
            m2=mm[i+1];  
            r2=rr[i+1];     
            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];  
                __int64 ans=1;  
                for (int i=1;i<N;i++)  
                    r1=gcd(mm[i],r1);  
                for (int i=0;i<N;i++)  
                    ans*=mm[i];  
                r1=ans/r1;  
            }  
            if (r1==0&&N==1)  
                r1=mm[0];  
            printf ("Case %d: %I64d\n",Case,r1);  
        }  
    }  
    return 0;  
}  

参考:http://blog.csdn.net/mishifangxiangdefeng/article/details/7109217