-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva10533.cpp
53 lines (46 loc) · 861 Bytes
/
uva10533.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
#include <cstdio>
#include <cmath>
#include <cstring>
const int MAXN = 1000010;
bool u[MAXN];
int su[MAXN];
int res[MAXN];
int num;
void sieve_prime(int n) {
num = 1;
memset(u, true, sizeof(u));
for(int i = 2; i <= n; ++i) {
if(u[i]) su[num++] = i;
for(int j = 1; j < num; ++j) {
if(i*su[j] > n) break;
u[i*su[j]] = false;
if(i % su[j] == 0) break;
}
}
}
bool isdigitprime(int n) {
int v = 0;
while(n > 0) {
v += n %10;
n /= 10;
}
return u[v];
}
int main() {
int n;
n = 1000000;
sieve_prime(n);
memset(res, 0, sizeof(res));
for(int i = 2; i < MAXN; ++i) {
res[i] += res[i-1];
if(u[i] && isdigitprime(i)) ++res[i];
}
int ncase;
scanf("%d", &ncase);
int from, to;
while(ncase--) {
scanf("%d %d", &from, &to);
printf("%d\n", res[to] - res[from-1]);
}
return 0;
}