-
Notifications
You must be signed in to change notification settings - Fork 7
/
nonprimefactors.cpp
41 lines (35 loc) · 968 Bytes
/
nonprimefactors.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
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int limit = 2000000;
int numDiv[limit + 1];
int numDiffPF[limit + 1];
for (int i = 0; i <= limit; i++) {
numDiv[i] = 1;
numDiffPF[i] = 0;
}
for (int p = 2; p <= limit; p++) {
if (!numDiffPF[p]) { // p is guaranteed a prime
for (int i = 2*p; i <= limit; i += p) { // all multiples of p up to limit
numDiffPF[i]++;
int temp = i, exp = 1;
while (temp % p == 0) {
temp /= p;
exp++;
}
numDiv[i] *= exp;
}
}
}
int q, x;
cin >> q;
while (q--) {
cin >> x;
cout << numDiv[x] - numDiffPF[x] << '\n'; // endl is making it slow
}
return 0;
}