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

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

思路,不互质的中国剩余定理,无解输出-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\n");
        else if(a1!=0)
        printf("%lld\n",a1);
        else
        printf("%lld\n",b1);
    }
    return 0;
}
时间: 2024-11-04 20:45:27

hdu 3579 Hello Kiki 不互质的中国剩余定理的相关文章

hdu 1573 X问题 两两可能不互质的中国剩余定理

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod a[i] = b[i], … (0 < a[i] <= 10). Input 输入数据的第一行为一个正整数T,

poj 2891 模数不互质的中国剩余定理

Strange Way to Express Integers Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following: Choose k different positive integers a1, a2, -, ak. For some

HDU——3579 Hello Kiki

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4206    Accepted Submission(s): 1616 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins

HDU 3579 Hello Kiki 中国剩余定理(合并方程

题意: 给定方程 res % 14 = 5 res % 57 = 56 求res 中国剩余定理裸题 #include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #include<math.h> #include<set> #include<queue> #include<vector> using namespace s

hdu 3579 Hello Kiki (中国剩余定理)

Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1943    Accepted Submission(s): 693 Problem Description One day I was shopping in the supermarket. There was a cashier counting coins s

hdu 1573 X问题 (非互质的中国剩余定理)

X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2980    Accepted Submission(s): 942 Problem Description 求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], …, X mod

HDU 1573 X问题 中国剩余定理

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1573 题意:求在小于等于N的正整数中有多少个X满足:X mod a[0] = b[0], X mod a[1] = b[1], X mod a[2] = b[2], -, X mod a[i] = b[i], - (0 < a[i] <= 10). 思路:中国剩余定理的模板题,如果找不到这样的数或者最小的X大于N,输出零. 代码: #include <iostream> #include

hdu 3579 Hello Kiki

http://acm.hdu.edu.cn/showproblem.php?pid=3579 注意下最后的答案等于0是不行的,因为要的是正整数 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define inf (0x3f3f3f3f) typedef long

HDU5668 Circle 非互质中国剩余定理

分析:考虑对给定的出圈序列进行一次模拟,对于出圈的人我们显然可以由位置,编号等关系得到一个同余方程 一圈做下来我们就得到了n个同余方程 对每个方程用扩展欧几里得求解,最后找到最小可行解就是答案. 当然不要忘了判无解的情况. 有非常多选手似乎都是一眼标算然后写挂了,对此表示很遗憾,但是此题确实是比较容易写挂的... 注:中国剩余定理 解模线性方程组的时候 有两种情况 1:一种是模数是两辆互质的,这样的题可以用LRJ白书上的模板,俗称CRT1 2:模数存在不互质的,这样的需要用合并方程的做法,俗称C