-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEuler Totient Function.cpp
More file actions
41 lines (40 loc) · 894 Bytes
/
Euler Totient Function.cpp
File metadata and controls
41 lines (40 loc) · 894 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
#include <vector>
#include <math.h>
using namespace std;
typedef long long ll;
vector<int> factor(int n) {
vector<int> f;
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
f.push_back(i);
while (n % i == 0) {
n /= i;
}
}
}
if (n != 1) f.push_back(n);
return f;
}
int phi(int n) {
vector<int> p = factor(n);
for (int i = 0; i < (int)p.size(); i++) {
if (i && p[i] == p[i - 1]) continue;
n /= p[i];
n *= p[i] - 1;
}
return n;
}
ll sumDiv(ll N) {
ll PF_idx = 0, PF = primes[PF_idx], ans = 1; // start from ans = 1
while (N != 1 && (PF * PF <= N)) {
ll power = 0;
while (N % PF == 0) {
N /= PF;
power++;
}
ans *= ((ll)pow((double)PF, power + 1.0) - 1) / (PF - 1); // formula
PF = primes[++PF_idx];
}
if (N != 1) ans *= ((ll)pow((double)N, 2.0) - 1) / (N - 1); // last one
return ans;
}