On the math lesson a teacher asked each pupil to come up with his own lucky numbers. As…
- User
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.
Examples
Input
3 5
Output
1
Input
6 66
Output
7
Assistant
#include <iostream> #include <vector> #include <cmath> #include <algorithm> using namespace std; int main() { long long L, R; cin >> L >> R; if (R < 2) { cout << 0 << endl; return 0; } // Compute primes up to sqrt(R) int max_small = sqrt(R); vector<bool> is_prime_small(max_small + 1, true); is_prime_small[0] = is_prime_small[1] = false; vector<int> primes; for (int p = 2; p <= max_small; ++p) { if (is_prime_small[p]) { primes.push_back(p); for (int multiple = p * p; multiple <= max_small; multiple += p) { is_prime_small[multiple] = false; } } } // Segmented sieve for the range [L, R] long long delta = R - L + 1; vector<bool> is_prime(delta, true); // Handle numbers less than 2 for (int i = 0; i < delta; ++i) { if (L + i < 2) { is_prime[i] = false; } } for (int p : primes) { if (p * p > R) continue; long long first = ((L + p - 1) / p) * p; if (first < L) { first += p; } if (first > R) continue; int idx = first - L; for (int i = idx; i < delta; i += p) { is_prime[i] = false; } } // Count valid primes int count = 0; for (int i = 0; i < delta; ++i) { if (is_prime[i]) { long long num = L + i; if (num == 2 || (num % 4) == 1) { count++; } } } cout << count << endl; return 0; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.