forked from cp-algorithms/cp-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_chinese_remainder_theorem.cpp
More file actions
51 lines (45 loc) · 967 Bytes
/
test_chinese_remainder_theorem.cpp
File metadata and controls
51 lines (45 loc) · 967 Bytes
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
42
43
44
45
46
47
48
49
50
51
#include <cassert>
#include <vector>
using namespace std;
int extended_euclidean(int a, int b, int &x, int &y) {
if (b == 0) {
x = 1;
y = 0;
return a;
}
int x1, y1;
int d = extended_euclidean(b, a % b, x1, y1);
x = y1;
y = x1 - y1 * (a / b);
return d;
}
long long mod_inv(long long a, long long m) {
int x, y;
int g = extended_euclidean(a, m, x, y);
assert(g == 1);
return (x % m + m) % m;
}
#include "chinese_remainder_theorem.h"
int main() {
// Sunzi Suanjing problem
vector<Congruence> C1{
{2, 3},
{3, 5},
{2, 7},
};
assert(chinese_remainder_theorem(C1) == 23);
// example 5 from
// https://www.math.cmu.edu/~mradclif/teaching/127S19/Notes/ChineseRemainderTheorem.pdf
vector<Congruence> C2{
{2, 5},
{3, 7},
{10, 11},
};
// example 6 from same PDF
vector<Congruence> C3{
{3, 7},
{3, 5},
{4, 12},
};
assert(chinese_remainder_theorem(C3) == 388);
}