本文参考于https://www.cnblogs.com/nowandforever/p/4515612.html类似问法给定整数a和b请问区间[a,b)内有多少素数a b 1012b - a 106因为b以内的合数的最小质因数一定不是超过sqrt(b)如果有sqrt(b)以内的素数表的话就可以把筛选法用在[a,b)上了先分别做好[2,sqrt(b))的表和[a,b)的表然后从[2,sqrt(b))的表中筛得素数的同时也将其倍数从[a,b)的表中划去最后剩下的就是区间[a,b)内的素数了。有的时候需要求出某个特定区间的素数但是数可能很大数组也要开不小所以需要进行下标转移这样才可以使用筛选法。#definell long longconstintmaxn1000010;boolpri[maxn],prii[maxn];ll prime[maxn];intcnt;//区间[a,b)内的整数筛法prii[i-a]true---代表i是素数注意这里下标偏移了a,所以从0开始//求解[a,b]内就把 全改为 voidseg_pri(ll a,ll b){cnt0;memset(pri,false,sizeof(pri));for(ll i0;i*ib;i)prii[i]true;//对[2,sqrt(b))的初始化全为素数for(ll i0;ib-a;i)pri[i]true;//求下表偏移后的[a,b)进行初始化for(ll i2;i*ib;i){if(prii[i]){for(ll j2*i;j*jb;ji)prii[j]false;//筛选[2,sqrt(b))//(ai-1)/i得到最接近a的i的倍数最低是i的2倍然后筛选for(ll jmax(2ll,(ai-1)/i)*i;jb;ji)pri[j-a]false;}}for(ll i0;ib-a;i){//统计去1if(pri[i]ia1)prime[cnt]ia;}/*for(int i0;icnt;i) coutprime[i]endl; coutcntendl;*/}传送门POJ 2689 Prime Distancehttp://poj.org/problem?id2689DescriptionThe 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 (1L U2,147,483,647), and you are to find the two adjacent primes C1 and C2 (LC1 C2U) 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 (LD1 D2U) where D1 and D2 are as distant from each other as possible (again choosing the first pair if there is a tie).InputEach 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.OutputFor 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 Input2 1714 17Sample Output2,3 are closest, 7,11 are most distant.There are no adjacent primes.题意输入整数LU(1L U2,147,483,647)在[L,U]区间内找相邻素数之差最小的素数对最大的素数对并输出相同者取第一组如果区间内素数个数 2 输出 There are no adjacent primes.思路先区间筛得到素数表在跑一边素数表找就可以了#includecstdio#includeiostream#includecmath#includealgorithm#includecstring#includecstdlib#includecctype#includevector#includestack#includequeue#includectime#definell long long#defineld long double#defineull unsigned long longusingnamespacestd;constintmaxn1000010;constintinf0x3f3f3f3f3f;constll lnf0x3f3f3f3f3f3f3f;boolpri[maxn],prii[maxn];ll prime[maxn],n,m;intcnt;voidseg_pri(ll a,ll b){cnt0;memset(pri,false,sizeof(pri));for(ll i0;i*ib;i)prii[i]true;for(ll i0;ib-a;i)pri[i]true;for(ll i2;i*ib;i){if(prii[i]){for(ll j2*i;j*jb;ji)prii[j]false;//用 ll , 不然 REfor(ll jmax(2ll,(ai-1)/i)*i;jb;ji)pri[j-a]false;}}for(ll i0;ib-a;i){if(pri[i]ia1)prime[cnt]ia;//注意去1}}intmain(void){ll mi,mil,mir;ll ma,mal,mar;while(~scanf(%lld%lld,n,m)){seg_pri(n,m);milnf,ma0;if(cnt2){puts(There are no adjacent primes.);continue;}for(inti1;icnt;i){ll dprime[i]-prime[i-1];if(dmi){mid;milprime[i-1];mirprime[i];}if(dma){mad;malprime[i-1];marprime[i];}}printf(%lld,%lld are closest, ,mil,mir);printf(%lld,%lld are most distant.\n,mal,mar);}return0;}