0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-14 02:58:47 +00:00
OI-codes/Luogu/P3868/P3868.cpp

60 lines
980 B
C++
Raw Normal View History

#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int K = 15;
int k;
long long a[K], b[K];
long long exgcd(long long a, long long b, long long &x, long long &y) {
if (!b) {
x = 1, y = 0;
return a;
}
long long g = exgcd(b, a % b, y, x);
y -= a / b * x;
return g;
}
long long CRT() {
long long mod = 1, ans = 0;
for (int i = 1; i <= k; i++) {
mod *= b[i];
}
for (int i = 1; i <= k; i++) {
long long m = mod / b[i], x, y;
exgcd(m, b[i], x, y);
ans = (ans + static_cast<__int128>(a[i]) * m * x % mod) % mod;
}
return (ans % mod + mod) % mod;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> k;
for (int i = 1; i <= k; i++) {
cin >> a[i];
}
for (int i = 1; i <= k; i++) {
cin >> b[i];
a[i] = (a[i] % b[i] + b[i]) % b[i];
}
cout << CRT() << endl;
return 0;
}