-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoPrime.cpp
More file actions
97 lines (72 loc) · 1.44 KB
/
Copy pathCoPrime.cpp
File metadata and controls
97 lines (72 loc) · 1.44 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
number of coprime of n using sieve only
*/
#include<bits/stdc++.h>
using namespace std;
#define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MX=10000000;
int co[MX+5], p[MX+5];
void coprime()
{
for(int i=1; i<=MX; i++) co[i]=i;
for(int i=2; i<=MX; i++)
{
if(!p[i])
{
for(int j=i; j<=MX; p[j]=1, j+=i)
{
co[j] = ( co[j] / i ) * (i-1);
}
}
}
}
int main()
{
FasterIO;
coprime();
return 0;
}
/*
number of coprime of n using sieve and prime factorization
*/
#include<bits/stdc++.h>
using namespace std;
#define FasterIO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int MX=10000000;
int mark[MX+5],co[MX+5];
vector<int>pm;
void sieve()
{
for(int i=2; i*i<=MX; i++)
{
if(mark[i]==0)
{
for(int j=i+i;j<=MX;j+=i) mark[j]=1;
}
}
for(int i=2; i<=MX; i++) if(!mark[i]) pm.push_back(i);
}
int co_fact(int x)
{
int xx=x;
for(int i=0; pm[i]*pm[i]<=x; i++)
{
if(x%pm[i]==0)
{
xx/=pm[i]; xx*=(pm[i]-1);
while(x%pm[i]==0) x/=pm[i];
}
}
if(x>=2)
{
xx/=x; xx*=(x-1);
}
return xx;
}
int main()
{
ll tc,n,i,j;
sieve();
for(int i=1; i<=MX; i++) co[i]=co_fact(i);
return 0;
}