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
Question meaning: Give you a variety of different ways to count money, and find the minimum amount of money that meets the requirements.
Idea: At first glance, it seems to be a question about the Chinese Remainder Theorem, but the modulus of this question is not necessarily pairwise reciprocal prime. Therefore, it can be done by solving the system of modular linear equations, which requires understanding the extended Euclidean algorithm. Let's talk about the method of solving the system of modular linear equations. The idea is: continue to combine the two to obtain. We can first find two congruence equations and let the general solution be N, N=r1(mod(m1)), N=r2(mod(m2)), which can obviously be converted into k1*m1+r1=k2*m2+r2 ;--->k1*m1+(-k2*m2)=r2-r1; Assume a=m1,b=m2,x=k1,y=(-k2),c=r2-r1 equation can be written as ax +by=c; just solve x by extended Euclid, then turn x into the smallest positive integer solution of the original equation, (x*(c/d)%(b/d)+(b/d) )%(b/d); then this x is the smallest integer solution of the original equation. So N=a*(x+n*(b/d))+r1====N=(a*b/d)*n+(a*x+r1), here only n is the unknown number, so it is another The formula of N=(a*x+r1)(mod(a*b/d)), and then as long as you continue to turn the two formulas into one formula, you can finally solve the solution of this system of equations
AC code:
[cpp]
#include
#include
#include
#include
#define N 7
using namespace std;
int M[N],A[N];
int Gcd(int a,int b)
{return b==0?a:Gcd(b,a%b);}
void gcd(int a,int b,int &d,int &x,int &y)
{
If(!b) x=1,y=0,d=a;
else gcd(b,a%b,d,y,x),y-=a/b*x;
}
int main()
{
int T;
Scanf("%d",&T);
for(int k=1;k<=T;++k)
{
int n;
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 x,y,d;
int a=M[0],c1=A[0];
bool flag=false;
for(int i=1;i
int c=A[i]-c1;
gcd(a,b,d,x,y);
If(c%d){flag=true;break;}
int r=b/d;
x=(c/d*x%r+r)%r;
c1=a*x+c1;
a=a*r;
}
If(flag) printf("Case %d: -1n",k);
else
int ans=1;
If(c1==0)//Special case when all remainders are 0
for(int i=0;i!=n;++i)
ans=M[i]/Gcd(ans,M[i])*ans;
printf("Case %d: %dn",k,ans);
else printf("Case %d: %dn",k,c1);
}
}return 0;
}
Author: smallacmer