-
Notifications
You must be signed in to change notification settings - Fork 0
/
0165.cpp
45 lines (40 loc) · 980 Bytes
/
0165.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
int main(void){
int n;
bool isprime[1000001];
vector<int> primes;
//sieve
memset(isprime, true, sizeof(isprime));
for(int i=2;i<=1000000;i++){
if(isprime[i] == true){
primes.push_back(i);
for(int j=2;i*j<=1000000;j++){
isprime[i*j] = false;
}
}
}
int prize,ret,p,m;
while(cin >> n && n){
ret = 0;
prize = 0;
for(int i=0;i<n;i++){
cin >> p >> m;
prize = upper_bound(primes.begin(), primes.end(),p+m) -
lower_bound(primes.begin(),primes.end(),p-m);
if(prize == 0){
ret -= 1;
}else{
ret += prize-1;
}
}
if(ret <= 0){
cout << 0 << endl;
}else{
cout << ret << endl;
}
}
}