D - 2017-like Number
Time limit : 2sec / Memory limit : 256MB
Score : 400 points
Problem Statement
We say that a odd number N is similar to 2017 when both N and (N+1)⁄2 are prime.
You are given Q queries.
In the i-th query, given two odd numbers li and ri, find the number of odd numbers x similar to 2017 such that li≤x≤ri.
Constraints
- 1≤Q≤105
- 1≤li≤ri≤105
- li and ri are odd.
- All input values are integers.
Input
Input is given from Standard Input in the following format:
Ql1 r1:lQ rQ
Output
Print Q lines. The i-th line (1≤i≤Q) should contain the response to the i-th query.
Sample Input 1
Copy
13 7
Sample Output 1
Copy
2
- 3 is similar to 2017, since both 3 and (3+1)⁄2=2 are prime.
- 5 is similar to 2017, since both 5 and (5+1)⁄2=3 are prime.
- 7 is not similar to 2017, since (7+1)⁄2=4 is not prime, although 7 is prime.
Thus, the response to the first query should be 2.
Sample Input 2
Copy
413 137 117 112017 2017
Sample Output 2
Copy
1001
Note that 2017 is also similar to 2017.
Sample Input 3
Copy
61 5313 9137 5519 5173 9113 49
Sample Output 3
Copy
441112 【题意】:当N和(N + 1)/ 2都是素数时,奇数N与2017相似。 你有Q个查询。 在第i个查询中,给定两个奇数Li和Ri,找到与2017相似的奇数x的数目,使得li≤x≤ri。 【分析】:筛素数用埃筛,查询用前缀和。 【代码】:
#includeusing namespace std;bool f [100001];int c [100002];int N,L,R ;int main (){ for (int i =2; i<=100000; i++) if (!f[i]) for (int j = i+i ;j<=100000; j+=i ) f[j]= true ; for (int i =3; i<=100000; i+=2) if (!f[i] && !f[(i+1)/2]) c[i]++; for (int i =3; i <=100000; i ++) c[i]+=c[i-1]; scanf ("%d",&N); while (N--) { scanf ("%d%d" ,&L ,&R ); printf ("%d\n" , c[R] - c[L-1]); }}