Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4559 Accepted Submission(s): 1763
Total Submission(s): 4559 Accepted Submission(s): 1763
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.
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
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
这也是一道中国剩余定理模板题(非互质)
对以下方程:
x = k1m1 + a1; (1)
x = k2m2 + a2;
有 k1m1 + a1 = k2m2 + a2; (2)
得 k1m1 + k2m2 = a2 - a1;
只需找到一个满足(2)式的最小正整数k1,将其代入(1)即可
则可对上式进行扩展欧几里得
设 d = gcd(m1, m2), c = a2 - a1
若 c%d == 0, 则 存在整数k1 = c/d*k1满足(2)式
为防止k1为负数, 令t = m2/d;
k1 = (k1%t + t)%t;
代入(1)式, 得x;
附模板代码:
int solve(int n)
{
int m1, m2, a1, a2, d, x1, x2, c;
m1 = m[0];
a1 = a[0];
for(int i = 0; i < n-1; i++)
{ //cout<<a1<<endl;
m2 = m[i+1];
a2 = a[i+1];
exgcd(m1, m2, d, x1, x2);
c = a2- a1;
if(c%d)
{
flag = 1;
// cout<<a1<<endl;
break;
}
x1 = c/d*x1;
m2 /= d;
x1 = (x1%m2 + m2)%m2;
a1 += m1*x1;
m1 *= m2;
}
// cout<<a1<<endl;
return a1;
}
题目代码:
#include<bits/stdc++.h>
using namespace std;
int a[10];
int m[10];
int flag = 0;
void exgcd(int a, int b, int &d, int &x, int &y )
{
if(b == 0)
{
d = a;
x = 1;
y = 0;
return ;
}
exgcd(b, a%b, d, y, x);
y -= (a/b)*x;
}
int gcd(int a, int b)
{
return b == 0?a:gcd(b, a%b);
}
int solve(int n)
{
int m1, m2, a1, a2, d, x1, x2, c;
m1 = m[0];
a1 = a[0];
for(int i = 0; i < n-1; i++)
{ //cout<<a1<<endl;
m2 = m[i+1];
a2 = a[i+1];
exgcd(m1, m2, d, x1, x2);
c = a2- a1;
if(c%d)
{
flag = 1;
// cout<<a1<<endl;
break;
}
x1 = c/d*x1;
m2 /= d;
x1 = (x1%m2 + m2)%m2;
a1 += m1*x1;
m1 *= m2;
}
// cout<<a1<<endl;
return a1;
}
int main()
{
int t, n;
scanf("%d", &t);
for(int k = 1; k <= t; k++)
{
flag = 0;
scanf("%d", &n);
for(int i = 0 ; i < n; i++)
{
scanf("%d", &m[i]);
}
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
int ans = solve(n);
// cout<<"flag = "<<flag<<endl;
if(flag)
cout<<"Case "<<k<<": -1"<<endl;
else
{
if(ans == 0 && n > 1)
{
ans = m[0];
int cnt = 1;
for(int i = 1; i < n; i++)
{
ans = gcd(m[i], ans);
}
for(int i = 0; i < n; i++)
{
cnt *= m[i];
}
ans = cnt/ans;
}
if(ans == 0 && n == 1)
ans = m[0];
cout<<"Case "<<k<<": "<<ans<<endl;
}
}
return 0;
}