素数筛选(2次):poj2689 Prime Distance
来源:互联网 发布:网络域名诈骗 编辑:程序博客网 时间:2024/05/29 17:45
Prime Distance
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 1714 17
Sample Output
2,3 are closest, 7,11 are most distant.There are no adjacent primes.解题思路:
首先,不同于普通素数筛选,由于上限U很大,只进行一次从2-2147483647的筛选绝对会超时,跟不行不通,除非改进算法,否则就要换一下思路。
然后,可以考虑只对L-U内的素数进行筛选,筛选的方法是从1-sqrt(2147483647)内的素数筛选(素因子肯定不超过sqrt(2147483647)),getprime()函数实现。
接着,用可能的素因子去筛选L-U区间的数,和筛法的思想相同(但要注意从哪开始,begin怎么确定),将所有的素数存到数组q[]中。
最后,for循环一次,找到相邻的q[i+1]、q[i]的最大值和最小值,输出。
思路并不难,最蹩脚的是数据的溢出问题。
①for(int i=l;i<=u;i++) 当i=u=2147483647时,i++会溢出,进入死循环,所以要处理一下。
②long long begin=l%p[i]?l+(p[i]-l%p[i]):l+l%p[i];在确定begin时,有加法可能会溢出,可以用long long p[]和long long begin,也可以都换成long long或unsigned int
参考代码和部分注释:
0 0
- 素数筛选(2次):poj2689 Prime Distance
- poj2689 Prime Distance 素数筛选应用 复习
- POJ2689 Prime distance 素数
- POJ2689:Prime Distance(大区间素数)
- POJ2689 Prime Distance 素数距离
- ZOJ 1842Prime Distance(素数筛选法2次使用)
- Prime Distance(大素数筛选)
- Prime Distance(筛选法求素数 )
- POJ2689 Prime Distance
- poj2689 Prime Distance
- POJ2689 Prime Distance
- poj2689 Prime Distance
- 【数论】poj2689 Prime Distance
- POJ2689 Prime Distance
- poj2689 Prime Distance
- POJ2689 Prime Distance
- poj2689 Prime Distance
- Prime Distance(poj2689变形的筛法素数)
- UI基础第十三弹: UIScrollView控件
- mysql优化-索引
- Mahout fp-growth
- andorid sdk manager代理服务器设置
- 漫画:程序员的工作
- 素数筛选(2次):poj2689 Prime Distance
- MyEclipse一个jsp文件红叉,但是打开文件后却找不到错误
- AndroidDevTools
- 【将金令】1.20晚间:欧银咄咄逼人,金银多头开炮
- 黑马程序员-⑥核心语法(下)
- ime-mode 禁止中文和中文符号输入 邮政编码,非负数
- Mahout系列之----kmeans 聚类
- vs2010远程调试
- 2015年1月17日--1月25日(共21小时,剩2/393小时)