Codeforces 113C

题目链接

C. Double Happiness
time limit per test
5 seconds
memory limit per test
128 megabytes
input
standard input
output
standard output

On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As a fan of number theory Peter chose prime numbers. Bob was more original. He said that number t is his lucky number, if it can be represented as:

t = a2 + b2, 
where a, b are arbitrary positive integers.

Now, the boys decided to find out how many days of the interval [l, r] (l ≤ r) are suitable for pair programming. They decided that the day i (l ≤ i ≤ r) is suitable for pair programming if and only if the number i is lucky for Peter and lucky for Bob at the same time. Help the boys to find the number of such days.

Input

The first line of the input contains integer numbers l, r (1 ≤ l, r ≤ 3·108).

Output

In the only line print the number of days on the segment [l, r], which are lucky for Peter and Bob at the same time.

Sample test(s)
input
3 5
output
1
input
6 66
output
7
关于bitset的使用,可以看这里
关于高斯整数,可以点这里

 1 /*************************************************************************
 2     > File Name: 113C.cpp
 3     > Author: Stomach_ache
 4     > Mail: sudaweitong@gmail.com
 5     > Created Time: 2014年07月20日 星期日 13时34分53秒
 6     > Propose: 
 7  ************************************************************************/
 8 
 9 #include <cmath>
10 #include <string>
11 #include <bitset>
12 #include <cstdio>
13 #include <fstream>
14 #include <cstring>
15 #include <iostream>
16 #include <algorithm>
17 using namespace std;
18 
19 const int maxn = 1e8+1e8+1e8;
20 bitset<maxn> p;
21 
22 int
23 main(void) {
24       int L, R;
25       scanf("%d %d
", &L, &R);
26     p.set();
27     for (int i = 3; i*i <= R; i+=2) if (p[i]) for (int j = i*i; j <= R; j+=2*i) p[j] = false;
28     int ans = 0;
29     for (int i = 5; i <= R; i+=4) if (p[i] && i >= L) ans++;
30     ans += (L <= 2 && R >= 2) ? 1 : 0;
31     printf("%d
", ans);
32 
33     return 0;
34 }



原文地址:https://www.cnblogs.com/Stomach-ache/p/3856190.html