-
Notifications
You must be signed in to change notification settings - Fork 0
/
etf.cpp
69 lines (68 loc) · 1.64 KB
/
etf.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
#include<cstdio>
#include<vector>
#include<cstring>
#include<set>
#include<cmath>
using namespace std;
int computeTotient(int n, vector<int>& primes) {
if(n == 1) {
return 1;
}
set<int> primeDivs;
long long ntmp = n;
for(int i=0;i < primes.size() && primes[i] <= ntmp;++i) {
if(n % primes[i] == 0) {
primeDivs.insert(primes[i]);
while(ntmp % primes[i] == 0) {
ntmp /= primes[i];
}
}
}
if(ntmp != 1) {
primeDivs.insert(ntmp);
}
long long prod = n;
for(set<int>::iterator itr=primeDivs.begin();itr != primeDivs.end();++itr) {
prod *= (*itr - 1);
}
for(set<int>::iterator itr= primeDivs.begin();itr != primeDivs.end();++itr) {
prod /= *itr;
}
return prod;
}
int main() {
int test;
scanf("%d", &test);
// compute primes using sieve
bool primeArray[1001];
memset(primeArray, 0, sizeof(primeArray));
primeArray[0] = primeArray[1] = true;
int len = sizeof(primeArray)/sizeof(bool);
int sqr = sqrt(len);
for(int i=3;i<=sqr;i+=2) {
if(primeArray[i]) {
continue;
}
for(int j=i;j<=sqr;++j) {
int prod = i*j;
if(i*j < len) {
primeArray[prod] = true;
} else {
break;
}
}
}
vector<int> primes;
primes.push_back(2);
for(int i=3;i<len;i+=2) {
if(!primeArray[i]) {
primes.push_back(i);
}
}
while(test--) {
int n;
scanf("%d", &n);
int val = computeTotient(n, primes);
printf("%d\n", val);
}
}