You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.0 KiB

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int N = 20;
int qmul(int a, int b, int mod) {
int res = 0;
while (b) {
if (b & 1) res = (res + a) % mod;
a = (a + a) % mod;
b >>= 1;
}
return res;
}
int n;
int a[N];
int m[N];
int A = 1;
int x, y;
int res;
int exgcd(int a, int b, int &x, int &y) {
if (!b) {
x = 1, y = 0;
return a;
}
int d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
void CRT() {
for (int i = 1; i <= n; i++) cin >> m[i];
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) {
m[i] = (m[i] % a[i] + a[i]) % a[i];
A *= a[i];
}
for (int i = 1; i <= n; i++) {
int Mi = A / a[i];
exgcd(Mi, a[i], x, y);
x = (x % a[i] + a[i]) % a[i];
res = (res + qmul(m[i], A / a[i] * x, A)) % A;
}
}
signed main() {
cin >> n;
CRT();
cout << res << endl;
}