0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P1495 【模板】中国剩余定理(CRT)/曹冲养猪

https://www.luogu.com.cn/record/74538615
This commit is contained in:
Baoshuo Ren 2022-04-24 21:09:45 +08:00
parent 004bd8f492
commit 4850a27856
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

52
Luogu/P1495/P1495.cpp Normal file
View File

@ -0,0 +1,52 @@
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
int n;
long long a[N], b[N];
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 <= n; i++) {
mod *= a[i];
}
for (int i = 1; i <= n; i++) {
long long m = mod / a[i], x, y;
exgcd(m, a[i], x, y);
ans = (ans + b[i] * m * x % mod) % mod;
}
return (ans % mod + mod) % mod;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i] >> b[i];
}
cout << CRT() << endl;
return 0;
}