Hello Kiki
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2270 Accepted Submission(s): 827
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
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
Recommend
只能说目前略懂,先收藏一下
ac代码
#include<stdio.h>
#include<string.h>
__int64 m[10],a[10];
__int64 e_gcd(__int64 a,__int64 b,__int64 &x,__int64 &y)
{
__int64 d;
if(b==0)
{
x=1;
y=0;
return a;
}
d=e_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
__int64 remainder(__int64 m[],__int64 a[],int n)
{
int w=0,i;
__int64 n1=m[0],b1=a[0],x,y;
for(i=1;i<n;i++)
{
__int64 n2=m[i],b2=a[i];
__int64 dd=b2-b1;
__int64 d=e_gcd(n1,n2,x,y);
if(dd%d)
{
w=1;
break;
}
__int64 k=dd/d*x;//k
__int64 t=n2/d;
if(t<0)
t=-t;
k=(k%t+t)%t;//k'
b1=b1+n1*k;
n1=n1/d*n2;
}
if(w)
return -1;
return b1;
}
int main()
{
int t,c=0;
scanf("%d",&t);
while(t--)
{
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%I64d",&m[i]);
for(i=0;i<n;i++)
scanf("%I64d",&a[i]);
if(n==1&&a[0]==0)
{
printf("Case %d: %I64d\n",++c,m[0]);
continue;
}
printf("Case %d: %I64d\n",++c,remainder(m,a,n));
}
}