-
Notifications
You must be signed in to change notification settings - Fork 0
/
10533 accepted.cpp
60 lines (54 loc) · 1.02 KB
/
10533 accepted.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
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#define N 1000000
using namespace std;
int status[N];
int dig[N];
int result[N];
void sieve(){
status[2]=1;
for(int i=3 ; i<=N ; i+=2)
status[i]=1;
for(int i=4 ; i<=N ; i+=2)
status[i]=0;
for(int i=3 ; i<sqrt(N) ; i+=2){
if(status[i]==1){
for(int j=i*i ; j<=N ; j+=i+i)
status[j]=0;
}
}
}
void DigitCheck(){
int sum,mod;
int a,b,n,res;
for(int i=0 ; i<1000000 ; i++){
sum=0;
int x=i;
while(x!=0){
mod=x%10;
x/=10;
sum+=mod;
}
if(status[sum]==1 && status[i]==1)
dig[i]=1;
}
int tot=0;
for(int j=0 ; j<1000000 ; j++){
tot+=dig[j];
result[j]=tot;
}
scanf("%d",&n);
for(int c=0 ; c<n ; c++){
scanf("%d %d",&a,&b);
res=result[b]-result[a-1];
printf("%d\n",res);
}
}
int main()
{
sieve();
DigitCheck();
return 0;
}