-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCD Extreme - Euler Totient Funtion
More file actions
47 lines (39 loc) · 990 Bytes
/
GCD Extreme - Euler Totient Funtion
File metadata and controls
47 lines (39 loc) · 990 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
42
43
44
45
46
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ;
int phi[1000000+10] ;
ll S[1000000+10] ;
void pre_calculation()
{
phi[1] = 1 ;
for(int i=2;i<=1000000;i++)
{
if(phi[i]==0)
{
phi[i] = i-1 ;
for(int j=i+i;j<=1000000;j+=i)
{
if(phi[j]==0) phi[j] = j ;
phi[j] = phi[j] - phi[j]/i ;
}
}
}
// For each Multiple of i pairng with all its multiple and making gcd with it is
// obiviously i so adding it to its respective multiple value
// phi[j/i] is due for phi[j/i] will give number of values which give GCD=i
for(int i=1;i<=1000000;i++)
{
for(int j=i+i;j<=1000000;j+=i)
{
S[j] = S[j] + (ll) (i*phi[j/i]);
}
}
for(int i=1;i<=1000000;i++) S[i] = S[i] + S[i-1] ;
}
int main()
{
pre_calculation();
int n ;
while(scanf("%d",&n)==1&&n!=0) printf("%lld\n",S[n]);
return 0;
}