Hello KikiTime

**limit:**MS

**Memory Limit:**32768KB

**64bit IO Format:**%i64d &%i64u SubmitStatusPracticeHDU 3579Appoint Description:System Crawler (2015-04-29)

Description

One day I is shopping in the supermarket. There is a cashier counting coins seriously when a little kid running and singing "in front of the bridge over a group of ducks, come quickly to count, 24678". And then the cashier put the counted coins back morosely and count again ...

Hello Kiki is such a lovely girl so she loves doing counting in a different the. For example, when she was counting X coins, she count them N times. Each time she divide the coins to several same sized groups and write down the group size Mi and the number of the Remai Ning coins Ai on her note.

One day Kiki's father found she note and he wanted to know what much coins Kiki was counting.

Input

The first line is T indicating the number of test cases.

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 is integers.

1 <= T <=, 1 <= N <= 6, 1 <= Mi <=, 0 <= Ai < mi

Output

For each case output the least positive integer X which Kiki is counting in the sample output format. If there is no solution then output-1.

Sample Input

Sample Output

First, we can find two congruence equations to set the general solution to N;

N=R1 (MOD (M1)), N=R2 (mod (m2));

Obviously can be converted into K1*M1+R1=K2*M2+R2;--->k1*m1+ (-k2*m2) =r2-r1;

Set a=m1,b=m2,x=k1,y= (-K2), C=r2-r1 equation can be written as ax+by=c;

X can be solved by Euclid, then X is the smallest positive integer solution of the original equation (x* (C/D)% (b/d) + (b/d))% (b/d);

To see the solution to the linear equation. So this x is the smallest integer solution to the original equation.

So n=a* (x+n* (b/d)) +r1====n= (a*b/d) *n+ (A*X+R1),

Only n is unknown, so it's a n= (A*X+R1) (a*b/d),

Then, as long as the two formula is changed into a formula, the solution of this equation group can be solved finally.

Topic Connection: http://acm.hdu.edu.cn/showproblem.php?pid=3579

Reprint Please specify the Source: Looking for Children & stars

Child's Column

#include <stdio.h> #define LL __int64void exgcd (ll a,ll b,ll& d,ll& x,ll& y) {if (!b) {d=a;x=1;y=0;} else {EXGCD (b,a%b,d,y,x); Y-=x* (A/b); }}ll gcd (LL a,ll b) {if (!b) {return A;} GCD (b,a%b);} LL m[55],a[55]; ll China (int r) {ll dm,i,a,b,x,y,d; LL c,c1,c2; A=M[0]; C1=A[0]; for (I=1; i<r; i++) {b=m[i]; C2=a[i]; EXGCD (A,b,d,x,y); C=C2-C1; if (c%d) return-1;//c must be a multiple of D, if not, then, certainly no solution dm=b/d; X= ((x* (C/D))%DM+DM)%dm;//guarantees that x is the remainder of the minimum positive//c/dm, and the coefficients expand the remainder to be c1=a*x+c1; A=A*DM; } if (c1==0)//remainder is 0, stating m[] is geometric series. And the remainder is 0 {c1=1; for (i=0;i<r;i++) C1=C1*M[I]/GCD (C1,m[i]); } return C1;} int main () {int t,n,t=0; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (int i=0;i<n;i++) {scanf ("%i64d", &m[i]); } for (int i=0;i<n;i++) {scanf ("%i64d", &a[i]); } LL Ans=china (n); printf ("Case%d:%i64d\n", ++t,ans); } return 0;} /*20214 575 56519 54 40 24 8011 2 36 20 76214 575 56219 5411 2*/

Hello Kiki (the Chinese remainder theorem hdu3579+ not coprime)