-
Notifications
You must be signed in to change notification settings - Fork 16
/
MicroprimePrime.cpp
56 lines (46 loc) · 1 KB
/
MicroprimePrime.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
#include <bits/stdc++.h>
#define lli long long int
#define MAX 1000001
using namespace std;
//VJ's Code
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t,l,r;
cin>>t;
bitset<MAX> IsPrime;
IsPrime[1] = 1; //0 then prime // 1 then non prime
for ( int i = 2 ; i*i <= (MAX) ; ++ i)
if(!IsPrime[i])
for ( int j = i*i ; j <= MAX ; j += i)
IsPrime[j] = 1;
vector<int> primePrime;
int cummulativeSum = 0;
for ( int i = 1 ; i < MAX ; ++ i)
{
if(!IsPrime[i])
cummulativeSum += 1;
if(!IsPrime[cummulativeSum])
primePrime.push_back(i);
}
while(t --)
{
cin>>l>>r;
int pos ;
auto it = lower_bound(primePrime.begin(), primePrime.end(), l);
if(it != primePrime.end())
pos = it - primePrime.begin();
else
continue;
auto itr = lower_bound(primePrime.begin(), primePrime.end(), r);
if(*itr != r)
itr --;
if(itr != primePrime.end())
cout<<(itr - primePrime.begin()) - pos + 1<<"\n";
else
continue;
}
return 0;
}