-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP1865 A % B Problem.cpp
65 lines (58 loc) · 1.09 KB
/
P1865 A % B Problem.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
61
62
63
64
65
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1000000;
const int maxprime = 1e6;
bool isPrime[maxn];
int d[maxn];
int prime[maxprime], n, m, t;
int lowbit(int x) {
return x&(-x);
}
void update(int x, int v) {
while(x<=m) {
d[x]+=v;
x+=lowbit(x);
}
}
int query(int x) {
register int sum = 0;
while(x) {
sum+=d[x];
x-=lowbit(x);
}
return sum;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
memset(isPrime, true, sizeof isPrime);
isPrime[0] = isPrime[1] = false;
for(int i=2; i<=m; i++) {
if(isPrime[i]) {
prime[++prime[0]] = i;
update(i, 1);
}
for(int j=1; j<=prime[0] && i*prime[j]<=m; j++) {
//if(isPrime[i*prime[j]]) update(i*prime[j], -1);
isPrime[i*prime[j]] = false;
if(i%prime[j]==0) break;
}
}
int l, r;
for(int i=0; i<n; i++) {
cin>>l>>r;
if(l<1||l>m) {
cout<<"Crossing the line"<<endl;
continue;
}
if(r<1||r>m) {
cout<<"Crossing the line"<<endl;
continue;
}
cout<<query(r)-query(l-1)<<endl;
}
return 0;
}