2014
11-27

# 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)

#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;
}