-
Notifications
You must be signed in to change notification settings - Fork 0
/
Euler Totient - Relative Prime Numbers.cpp
72 lines (65 loc) · 1.39 KB
/
Euler Totient - Relative Prime Numbers.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<bits/stdc++.h>
#define MAX 5000005
#define LL long long int
using namespace std;
int status[(MAX/32)+10], sz, relative[MAX];
vector<int>primelist;
bool check(int n, int pos) { return (bool)(n & (1<<pos)); }
int SET(int n, int pos){ return n=n|(1<<pos);}
void sieve()
{
int sqrtN=int (sqrt(MAX));
status[1>>5]=SET(status[1>>5],1&31);
for(int j=4; j<MAX; j=j+2)
status[j>>5]=SET(status[j>>5],j&31);
for(int i=3; i<=sqrtN; i=i+2)
{
if(check(status[i>>5],i&31)==0)
{
for(int j=i*i; j<=MAX; j= j + (i<<1))
{
status[j>>5]=SET(status[j>>5],j&31);
}
}
}
primelist.push_back(2);
for(int i=3; i<=MAX; i=i+2)
{
if(check(status[i>>5], i&31)==0)
{
primelist.push_back(i);
}
}
sz = primelist.size();
}
void PHI()
{
relative[0] = 0;
for(int i=1; i<MAX; i++)
{
relative[i] = i;
}
for(int i=2; i<MAX; )
{
if(check(status[i>>5], i&31)==0 && relative[i]==i)
{
for(int j=i; j<MAX; j = j + i)
{
relative[j] = relative[j] - (relative[j] / i);
}
}
if(i>2) i = i + 2;
else i++;
}
}
int main()
{
sieve();
PHI();
long long int n;
while(scanf("%lld", &n))
{
printf("%lld\n", (LL) relative[n]);
}
return 0;
}