POJ 2689 - Prime Distance - [埃篩]
阿新 • • 發佈:2019-04-08
only namespace sting typedef led print it is visible define
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).
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.
題目鏈接:http://poj.org/problem?id=2689
Time Limit: 1000MS Memory Limit: 65536K
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.Input
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.
題意:
給出 $st$ 與 $ed$ ($1 \le st < ed \le 2147483647$ 且 $ed - st \le 1e6$),求 $[st,ed]$ 區間內,相鄰的兩個素數中,差最小的和差最大的(若存在差同樣大的一對素數,則有先給出最小的一對素數);
題解:
顯然,不可能直接去篩 $2147483647$ 以內的素數;
由於任何一個合數 $n$ 必定包含一個不超過 $\sqrt{n}$ 的質因子,
那麽,我們不妨先用歐拉篩法篩出 $[0,46341]$ 區間內的素數($46341 \approx \sqrt{2147483647}$);
然後對於每個test case的 $[st,ed]$ 區間,用篩出來的質數再去標記 $[st,ed]$ 內的合數,剩下來的就是質數了。
AC代碼:
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> #define pb(x) push_back(x) using namespace std; typedef long long ll; const int maxn=1e6+5; vector<ll> p; bool vis[maxn]; const int MAX=46350; bool noprm[MAX+3]; vector<int> prm; void Erato() { noprm[0]=noprm[1]=1; for(int i=2;i<=MAX;i++) { if(noprm[i]) continue; prm.pb(i); for(int j=i;j<=MAX/i;j++) noprm[i*j]=1; } } int main() { Erato(); ll L,R; while(cin>>L>>R) { for(ll i=L;i<=R;i++) vis[i-L]=0; for(int i=0;i<prm.size();i++) { ll st=max(2LL,L/prm[i]+(L%prm[i]>0)), ed=R/prm[i]; for(ll k=st;k<=ed;k++) vis[k*prm[i]-L]=1; } if(L==1) vis[0]=1; p.clear(); for(ll i=L;i<=R;i++) if(!vis[i-L]) p.pb(i); if(p.size()<=1) cout<<"There are no adjacent primes.\n"; else { int mn=1, mx=1; for(int i=1;i<p.size();i++) { if(p[i]-p[i-1]<p[mn]-p[mn-1]) mn=i; if(p[i]-p[i-1]>p[mx]-p[mx-1]) mx=i; } printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n",p[mn-1],p[mn],p[mx-1],p[mx]); } } }
POJ 2689 - Prime Distance - [埃篩]