Prime Distance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 19952 | Accepted: 5357 |
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).
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.
Source
大致题意:每次给一个区间,要求给出这个区间相邻的差最小和最大的素数对.
分析:一开始还以为l,u不超过1000000,这样的话直接筛就好了,没想到是不超过2,147,483,647,这样的话素数筛就筛不到这个范围了.但是每个区间都很小,需要把区间中的每个素数都给求出来才能继续做下去.那么利用筛法的原理,先筛出一小部分素数,然后将它在区间中的倍数给去掉.这里只需要筛出根号以内的素数就可以了.不过筛的话这个区间的数还是存不下,那么就要将整个区间偏移l,在小区间里求出来答案以后再往回偏移l,就能够得到答案了.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100000; int l,r,prime[1000100],tot,vis[1000010],cur,cur2,p1,p2,ans1,ans2,x3,y3,x4,y4; bool flag[1000010]; void init() { for (int i = 2; i <= maxn; i++) { if (!vis[i]) prime[++tot] = i; for (int j = 1; j <= tot; j++) { int t = prime[j] * i; if (t > maxn) break; vis[t] = 1; if (i % prime[j] == 0) break; } } } int main() { init(); while (scanf("%d%d",&l,&r) == 2 && (l || r)) { memset(flag,false,sizeof(flag)); ans1 = 100000000,ans2 = 0; for (int i = 1; i <= tot; i++) { int tl = (l - 1) / prime[i],tr = r / prime[i]; for (int j = max(2,tl); j <= tr; j++) flag[prime[i] * j - l] = 1; } int cur = -1; if (l == 1) flag[0] = 1; for (int i = 0; i <= r - l; i++) { if (!flag[i]) { if (cur != -1) { if (i - cur < ans1) { ans1 = i - cur; x3 = cur; y3 = i; } if (i - cur > ans2) { ans2 = i - cur; x4 = cur; y4 = i; } } cur = i; } } if (!ans2) cout<<"There are no adjacent primes."<<endl; else cout<<x3 + l<<","<<y3 + l<<" are closest, "<<x4 + l<<","<<y4 + l<<" are most distant."<<endl; } return 0; }