-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaps.cpp
39 lines (37 loc) · 875 Bytes
/
aps.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
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define MAX_VAL 10000001
long long sieve[MAX_VAL];
int main() {
int tests = 0;
scanf("%d", &tests);
// generate prime numbers upto 3200
int maxVal = sqrt(MAX_VAL);
memset(sieve, 0, sizeof(sieve));
sieve[0] = sieve[1] = 1;
for(int i=2;i<maxVal;++i) {
if(sieve[i]) {
continue;
}
for(int j=i;;++j) {
int prod = i*j;
if(prod >= MAX_VAL || prod <= 0) {
break;
}
if(!sieve[prod]) {
sieve[prod] = i;
}
}
}
sieve[0] = sieve[1] = 0;
for(int i=2;i<MAX_VAL;++i) {
sieve[i] = sieve[i-1] + (sieve[i]?sieve[i]:i);
}
while(tests--) {
int num;
scanf("%d", &num);
printf("%lld\n", sieve[num]);
}
}