-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.cpp
57 lines (49 loc) · 1.29 KB
/
source.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
// for GMP big integers beyond the range of long long replace the type with bigInteger
// #include <gmpxx.h>
// using bigInteger = mpz_class;
using namespace std;
typedef long long ll;
struct Triple {
ll d, x, y;
Triple(ll D, ll X, ll Y) : d(D), x(X), y(Y) {}
Triple() {}
void display() {
cout << "d, x, y: " << d << " " << x << " " << y << '\n';
}
};
Triple extendedGCD(ll a, ll b) {
Triple aux;
if (b == 0)
return Triple(a, 1, 0);
else
aux = extendedGCD(b, a % b);
return Triple(aux.d, aux.y, aux.x - (a / b) * aux.y);
}
ll inverse(ll a, ll n) {
Triple egcd = extendedGCD(a, n);
if (egcd.d != 1)
return -1; // no inverse exists in this ring
return (egcd.x % n + n) % n;
}
void linsolve(ll a, ll b, ll n) {
Triple gcd = extendedGCD(a, n);
if (b % gcd.d != 0) {
cout << "no solutions exist" << endl;
return;
}
ll a_prim = a / gcd.d;
ll b_prim = b / gcd.d;
ll n_prim = n / gcd.d;
ll x0 = (inverse(a_prim, n_prim) * b_prim) % n_prim;
for (ll i = 0; i < gcd.d; ++i) {
ll solution = (x0 + i * n_prim) % n;
cout << "solution: " << solution << endl;
}
}
int main() {
ll a, b, n;
cin >> a >> b >> n;
linsolve(a, b, n);
return 0;
}