-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrt.cpp
42 lines (34 loc) · 844 Bytes
/
crt.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
#include <bits/stdc++.h>
using namespace std;
int modpow(int a, int n, int mod) {
if (n == 0) return 1;
if (n % 2 == 0) {
int t = modpow(a, n / 2, mod);
return t * t % mod;
}
return a * modpow(a, n - 1, mod) % mod;
}
int modinv(int a, int mod) {
return modpow(a, mod - 2, mod);
}
pair<int, int> chrem(const vector<int> &ps, const vector<int> &rs) {
using Long = __int128_t;
int P = 1;
for (int p: ps) {
P *= p;
}
Long ret = 0;
for (int i = 0; i < ps.size(); i++) {
int p = P / ps[i];
ret += Long(1) * rs[i] * modinv(p, ps[i]) * p;
}
return make_pair(ret % P, P);
}
int main() {
vector<int> rs(3);
for (int i = 0; i < 3; i++) {
cin >> rs[i];
}
auto p = chrem({17, 107, (int) 1e9 + 7}, rs);
cout << p.first << '\n';
}