HDU 3579 Hello Kiki

来源:互联网 发布:软件试用期过了 编辑:程序博客网 时间:2024/05/19 18:39

Hello Kiki

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


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
2214 575 56519 54 40 24 8011 2 36 20 76
 

Sample Output
Case 1: 341Case 2: 5996


题目大意:KiKi有一堆硬币,她很喜欢数硬币,于是做了很多笔记,以不同的方法数硬币,分成Mi组就会剩余Ai个,让你求她最少有多少个硬币。


思路:中国剩余定理(扩展),其数据中余数不一定是互素的,因此需要将同余方程组合并来求解,最后要注意的是当余数都为0时需要特判,输出结果为几个数的最小公倍数lcm。


具体实现:

合并同余方程组的思路是两两合并,假设要合并一下两个方程组


                                

那么得到


       

在利用扩展欧几里得算法解出的最小正整数解,再代入

 

       

 

得到后合并为一个方程的结果为

 

       

 

这样一直合并下去,最终可以求得同余方程组的解。


ac代码:


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <stack>#include <vector>#include <cmath>#include <set>#include <map>#include <cstdlib>#include <functional>#include <climits>#include <cctype>#include <iomanip>using namespace std;typedef long long ll;#define INF 0x3f3f3f3f#define mod 1e9 + 7#define clr(a, x) memset(a, x, sizeof(a))const double eps = 1e-6;ll a[1005], m[1005];ll gcd(ll a, ll b){    return b == 0 ? a : gcd(b, a % b);}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;}ll Inv(ll a, ll b){    ll d = gcd(a, b);    if (d != 1)        return -1;    ll x, y;    exgcd(a, b, x, y);    return (x % b + b) % b;}bool merge(ll a1, ll m1, ll a2, ll m2, ll &a3, ll &m3){    ll d = gcd(m1, m2);    ll c = a2 - a1;    if (c % d)        return false;    c = (c % m2 + m2) % m2;    m1 /= d;    m2 /= d;    c /= d;    c *= Inv(m1, m2);    c %= m2;    c *= m1 * d;    c += a1;    m3 = m1 * m2 * d;    a3 = (c % m3 + m3) % m3;    return true;}ll crt(ll a[], ll m[], int n){    ll a1 = a[1];    ll m1 = m[1];    for (int i = 2; i <= n; i++)    {        ll a2 = a[i];        ll m2 = m[i];        ll m3, a3;        if (!merge(a1, m1, a2, m2, a3, m3))        {            return -1;        }        a1 = a3;        m1 = m3;    }    return (a1 % m1 + m1) % m1;}ll lcm(ll x, ll y){    return x / gcd(x, y) * y;}int main(){    int t;    int n;    int k = 0;    cin >> t;    while (t--)    {        int cnt = 0;        cin >> n;        for (int i = 1; i <= n; i++)            cin >> m[i];        for (int i = 1; i <= n; i++)            cin >> a[i];        printf("Case %d: ", ++k);        for (int i = 1; i <= n; i++)        {            if (a[i] == 0)                cnt++;        }        ll end = 1;        if (cnt == n)        {            end = lcm(end, m[1]);            for (int i = 2; i <= n; i++)            {                end = lcm(end, m[i]);            }            cout << end << endl;        }        else        {            ll ans = crt(a, m, n);            cout << ans << endl;        }    }    return 0;}