forked from roosephu/project-euler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path182.cpp
45 lines (37 loc) · 944 Bytes
/
182.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
#include <cstdio>
#include <algorithm>
using namespace std;
int cnt_p[10000], cnt_q[10000];
int gcd(int a, int b) {
return a ? gcd(b % a, a) : b;
}
int fpm(int b, int e, int m) {
int t = 1;
for (; e; e >>= 1, b = b * b % m)
e & 1 ? t = t * b % m : 0;
return t;
}
int main() {
int p = 1009, q = 3643;
// int p = 19, q = 37;
for (int e = 0; e < p - 1; ++e) {
for (int i = 0; i < p; ++i) {
cnt_p[e] += fpm(i, e, p) == i;
}
}
for (int e = 0; e < q - 1; ++e) {
for (int i = 0; i < q; ++i) {
cnt_q[e] += fpm(i, e, q) == i;
}
}
int phi = (p - 1) * (q - 1), min_ans = phi;
long long ans = 0;
for (int e = 2; e < phi; ++e)
if (gcd(e, phi) == 1)
min_ans = min(min_ans, cnt_p[e % (p - 1)] * cnt_q[e % (q - 1)]);
for (int e = 2; e < phi; ++e)
if (gcd(e, phi) == 1 && cnt_p[e % (p - 1)] * cnt_q[e % (q - 1)] == min_ans)
ans += e;
printf("%lld\n", ans);
return 0;
}