-
Notifications
You must be signed in to change notification settings - Fork 0
/
I. RSA.cpp
58 lines (49 loc) · 848 Bytes
/
I. RSA.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
58
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, E, C;
ll inv(ll a, ll b) {
return a > 1 ? b - inv(b % a, a) * b / a : 1;
}
map<ll, ll> facts(ll n) {
map<ll, ll> ans;
for (ll i = 2; i * i <= n; i++) {
ll count = 0;
for (; n % i == 0; count++, n /= i)
;
if (count) ans[i] = count;
}
if (n > 1) ans[n]++;
return ans;
}
ll fpow(ll x, ll y, ll m) {
ll ret = 1;
while (y) {
if (y & 1) ret = (ret * x) % m;
y >>= 1;
x = (x * x) % m;
}
return ret;
}
ll phi(ll n) {
if (n == 1) return 1;
auto fs = facts(n);
auto res = n;
for (auto [p, k] : fs) {
res /= p;
res *= (p - 1);
}
return res;
}
int main() {
cin >> N >> E >> C;
ll D = inv(E, phi(N));
ll m = fpow(C, D, N);
cout << m << endl;
}
/*
* AC
* Math
* Number Theory
* Phi function
* */