-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfractii.cpp
41 lines (31 loc) · 857 Bytes
/
fractii.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
#include <fstream>
#include <vector>
using namespace std;
vector<unsigned> genPhi(unsigned long long n) {
vector<unsigned> phi;
phi.resize(n+1);
for(unsigned long long i = 1; i <= n; i++)
phi[i]=i;
for(unsigned long long i = 2; i <= n; i++)
if(phi[i] == i)
for(unsigned long long j = i; j <= n; j += i) {
phi[j] /= i;
phi[j] *= (i-1);
}
return phi;
}
unsigned long long getNumFractions(unsigned long long n) {
vector<unsigned> phi(genPhi(n));
unsigned long long numFractions = 1;
for(unsigned long long i = 2; i <= n; ++i) {
numFractions += 2 * phi[i];
}
return numFractions;
}
int main() {
unsigned long long n;
ifstream in("fractii.in");
in >> n;
ofstream out("fractii.out");
out << getNumFractions(n);
}