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 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
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 3573 3574 3575 3576 3577
题意:Kiki有x个硬币,用了m种方法分,每次记录基数和剩余量,他爸爸呢,想要知道他的小金库有多少钱,但不会算,让你算一下;
裸中国剩余定理,互质与不互质并行
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<algorithm> #define N 20 using namespace std; int n,m[N],a[N],m1; int read() { int x=0,f=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1; ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘; ch=getchar();} return x*f; } int exgcd(int a,int b,int &x,int &y) { if(b==0) { x=1,y=0; return a; } int r=exgcd(b,a%b,x,y),tmp; tmp=x,x=y,y=tmp-a/b*y; return r; } int crt() { int a1=a[1],a2,m2,d,c;m1=m[1]; for(int i=2;i<=n;++i) { a2=a[i],m2=m[i]; c=a2-a1;int x=0,y=0; d=exgcd(m1,m2,x,y); if(c%d) return -1; x=x*c/d; int mod=m2/d; x=(mod+x%mod)%mod; a1+=m1*x;m1*=mod; } if(a1==0) a1+=m1; return a1; } int main() { int t,tot=0; t=read(); while(t--) { n=read(); for(int i=1;i<=n;++i) m[i]=read(); for(int i=1;i<=n;++i) a[i]=read(); printf("Case %d: %d\n",++tot,crt()); } return 0; }