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

Hello Kiki

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

 

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.

扫描二维码关注公众号,回复: 2602156 查看本文章

 

 

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

2010 ACM-ICPC Multi-University Training Contest(14)——Host by BJTU

 

 

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  3573 3574 3575 3576 3577 

算法分析:

差点疯了,今天下午无限wrong,原来中国剩余定理还有一种不互质的情况呀,吐血。

中国剩余定理讲解:点这里

本题给出不互质的模线性方程组,求出满足方程的最小正整数解
方案:对于不互质的模线性方程组,可以进行方程组合并,求出合并后的方程的解,这样就可以很快地推出方程的最终解。
两个方程合并的一种方法:
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))

 

代码实现:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#define mem(a,x) memset(a,x,sizeof(a))
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b == 0 ? a : gcd(b, a % b); }
ll e_gcd (ll a, ll b, ll& x, ll& y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    ll ans = e_gcd (b, a % b, y, x);
    y -= a / b * x;
    return ans;
}
ll CRT(int a[], int m[], int n)
{
    ll Mi = m[1], ans = a[1];
    for (int i = 2; i <= n; ++i)
    {
        ll mi = m[i], ai = a[i];
        ll x, y;
        ll gcd = e_gcd (Mi, mi, x, y);
        ll c = ai - ans;
        if (c % gcd != 0) return -1;
        ll M = mi / gcd;
        ans += Mi * ( ( (c /gcd*x) % M + M) % M);
        Mi *= M;
    }
    if (ans == 0) //当余数都为0
    {
        ans = 1;
        for (int i = 1; i <= n; ++i)
        {
            ans = ans*m[i]/gcd(ans,(ll)m[i]);
        }
    }
    return ans;
}
int main()
{
    int T;cin>>T;int kas = 0;
    while (T--)
    {
        int n,a[100],m[100];
        scanf("%d",&n);
        for (int i = 1;i <= n;++i) scanf("%d",&m[i]);
        for (int i = 1;i <= n;++i) scanf("%d",&a[i]);
        printf("Case %d: %lld\n",++kas,CRT(a,m,n));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sdz20172133/article/details/81459906
今日推荐