-
Notifications
You must be signed in to change notification settings - Fork 12
/
1117.cpp
60 lines (54 loc) · 1.03 KB
/
1117.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<cmath>
using namespace std;
typedef __int64 LL;
LL extgcd(LL a, LL b, LL &x, LL &y)
{
if(b==0)
{
x=1,y=0;
return a;
}
LL d = extgcd(b,a%b,x,y);
LL t = x;
x = y;
y = t - a/b*y;
return d;
}
//求解模线性方程组x mod ai =bi
LL China_Reminder(int len, LL* a, LL* n)
{
int i;
LL N = 1;
LL result = 0;
for(i = 0; i < len; i++)
N = N*n[i];
for(i = 0; i < len; i++)
{
LL m = N/n[i];
LL x,y;
extgcd(m,n[i],x,y);
x = (x%n[i]+n[i])%n[i];
result = (result + m*a[i]*x%N)%N;
}
return result;
}
LL a[1005],b[1005];
int main ()
{
int cas,n;
scanf("%d",&cas);
while(cas--)
{
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%I64d",&a[i]);
for(int i = 0; i < n; i++)
scanf("%I64d",&b[i]);
printf("%I64d\n",China_Reminder(n,b,a));
}
return 0;
}//Parsed in 0.095 seconds