-
Notifications
You must be signed in to change notification settings - Fork 14
/
Count of integers.cpp
58 lines (45 loc) · 1.23 KB
/
Count of integers.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
// BY AYUSH AKASH
#include <iostream>
#include <vector>
using namespace std;
int main()
{
const int MAX_N = 1e6 + 5;
vector <int> no_of_factors(MAX_N, 0);
for(int i = 1; i < MAX_N; i++)
{
for(int m = i; m < MAX_N; m += i)
{
no_of_factors[m]++;
}
}
vector <int> largest_prime_factor(MAX_N, 0);
vector <int> euler_totient(MAX_N, 0);
euler_totient[1] = 1;
for(int i = 2; i < MAX_N; i++)
{
if(largest_prime_factor[i] == 0)
{
for(int m = i; m < MAX_N; m += i)
{
largest_prime_factor[m] = i;
}
}
int p_power_n = 1, reduced_i = i;
while(reduced_i%largest_prime_factor[i] == 0)
{
reduced_i /= largest_prime_factor[i];
p_power_n *= largest_prime_factor[i];
}
euler_totient[i] = (p_power_n - p_power_n/largest_prime_factor[i])*euler_totient[reduced_i];
}
int no_of_queries;
cin >> no_of_queries;
while(no_of_queries--)
{
int n;
cin >> n;
cout << (n - no_of_factors[n] - euler_totient[n] + 1) << "\n";
}
return 0;
}