0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 21:28:48 +00:00

P3868 [TJOI2009] 猜数字

https://www.luogu.com.cn/record/89550624
This commit is contained in:
Baoshuo Ren 2022-10-12 11:35:54 +08:00
parent 059ef4d859
commit a5e7896fd9
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

59
Luogu/P3868/P3868.cpp Normal file
View File

@ -0,0 +1,59 @@
#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;
}