题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1842
Time Limit: 2 Seconds Memory Limit: 65536 KB
Problem Description
The branch of mathematics called number theory is about properties of numbers. One of the areas that has captured the interest of number theoreticians for thousands of years is the question of primality. A prime number is a number that is has no proper factors (it is only evenly divisible by 1 and itself). The first prime numbers are 2,3,5,7 but they quickly become less frequent. One of the interesting questions is how dense they are in various ranges. Adjacent primes are two numbers that are both primes, but there are no other prime numbers between the adjacent primes. For example, 2,3 are the only adjacent primes that are also adjacent numbers.
Your program is given 2 numbers: L and U (1 <= L < U <= 2,147,483,647), and you are to find the two adjacent primes C1 and C2 (L <= C1 < C2 <= U) that are closest (i.e. C2-C1 is the minimum). If there are other pairs that are the same distance apart, use the first pair. You are also to find the two adjacent primes D1 and D2 (L <= D1 < D2 <= U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).
Input
Each line of input will contain two positive integers, L and U, with L < U. The difference between L and U will not exceed 1,000,000.
Output
For each L and U, the output will either be the statement that there are no adjacent primes (because there are less than two primes between the two given numbers) or a line giving the two pairs of adjacent primes.
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes.
Problem solving report:
Description: 给你一个区间[L,R],让你找到其中相邻的两个素数中,最大的差和最小的差。
Problem solving: 虽然区间的范围比较大,但是考虑到其特性:R-L<=1e6.那么我们可以通过这个信息来用两次筛法得到区间内素数的信息。
根据我们熟知的理论,b以内的合数的最小质因数不会大于根号R,所以我们就可以筛出[2,sqrt(R)]内的素数,并用这些素数去筛出[L,R]之内的素数,然后暴力维护最大最小差即可。
Accepted Code:
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int MAXM = 1e6 + 5;
bool isprime[MAXM], prime[MAXM];
int cnt = 0, pre[MAXM];
void prime_() {
isprime[0] = isprime[1] = true;
for (int i = 2; i < MAXM; i++) {
if (!isprime[i])
pre[cnt++] = i;
for (int j = 0; j < cnt && i * pre[j] < MAXM; j++) {
isprime[i * pre[j]] = true;
if (!(i % pre[j]))
break;
}
}
}
void slove(int l, int r) {
memset(prime, false, sizeof(prime));
if (l == 1)
prime[0] = true;
int min_ = inf, max_ = -inf, min_a, min_b, max_a, max_b;
for (int i = 0; i < cnt && pre[i] <= r; i++) {
for (int j = (l - 1) / pre[i] + 1; j <= r / pre[i]; j++)
if (j != 1)
prime[j * pre[i] - l] = true;
}
for (int i = 0; i <= r - l; i++) {
if (!prime[i]) {
int j = i + 1;
while (j <= r - l && prime[j])
j++;
if (j > r - l)
break;
if (max_ < j - i) {
max_ = j - i;
max_a = i;
max_b = j;
}
if (min_ > j - i) {
min_ = j - i;
min_a = i;
min_b = j;
}
i = j - 1;
}
}
if (min_ < inf)
printf("%d,%d are closest, %d,%d are most distant.\n", min_a + l, min_b + l, max_a + l, max_b + l);
else printf("There are no adjacent primes.\n");
}
int main() {
prime_();
int l, r;
while (~scanf("%d%d", &l, &r))
slove(l, r);
return 0;
}