素数筛法
来源:互联网 发布:大数据hadoop入门 编辑:程序博客网 时间:2024/06/14 23:34
Prime Distance POJ - 2689
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.
题意:给出区间【l,r】,问其中距离最远的素数对
思路:先用o(n)筛素数法筛出10^6以内的素数,再用这些素数去筛大于10^6的数
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;__int64 tot, prime[1000000+5], vis[1000000+5], vis2[1000000+5];struct node{__int64 ll, rr, dis;}edge[1000000+5];void getprime(__int64 r) { tot = 0; memset(vis, 0, sizeof(vis)); for (__int64 i = 2; i <= r; i++) { if (!vis[i]) prime[tot++]=i; for (__int64 j = 0; j < tot && prime[j]*i <= r; ++j) { vis[prime[j]*i] = 1; if(i % prime[j] == 0) break; } } } int main(){__int64 l, r;while(~scanf("%I64d%I64d", &l, &r)){getprime(1000000);//0-prime__int64 i, j = 0, num = 0, sum = 0;int ant = 0;memset(vis2, 0, sizeof(vis2)); for(i=0; (__int64)prime[i]*prime[i]<=r; i++) //二次筛素数{ int k=l/prime[i]; if(k<=1)k=2; for(j = (__int64)prime[i]*k; j<=r; j+=prime[i]){ vis2[j-l]=1; } } //for(i = l; i<=r; i++) cout<<i<<" "<<vis2[i-l]<<endl;memset(edge, 0, sizeof(edge));j = 0;for(i = l; i <= r; i++){if(!vis2[i-l]&&i!=1){edge[j].dis = num;edge[j++].rr = i;edge[j].ll = i;num = 1;sum++;}else num++;}if(sum<2) printf("There are no adjacent primes.\n");else{__int64 mi = 1000000, ma = 0, p, q;for(i = 1; i < j; i++){if(edge[i].dis>ma){ma = edge[i].dis;q=i;}if(edge[i].dis<mi){mi = edge[i].dis;p=i;}}printf("%I64d,%I64d are closest, %I64d,%I64d are most distant.\n", edge[p].ll, edge[p].rr, edge[q].ll, edge[q].rr);}}return 0;}
- 又见素数(素数筛法)
- nyoj 26 孪生素数 素数筛法
- HDU 4548 美素数 // 素数筛法
- 素数判断和素数筛法
- 素数筛法-----》大规模的判断素数
- 素数筛(筛选法求素数)
- 素数筛选法(素数筛)
- 森德拉姆素数筛法
- 素数筛法
- 素数筛法
- 筛法素数表
- 筛素数法
- POJ2262 素数筛法
- POJ3306 素数筛法
- 素数筛法
- 素数筛法
- 素数筛法
- 【数学】素数筛法
- hdu1006 排序
- Java基础(二)
- 企业中MySQL主流高可用架构实战三部曲之MHA
- Java分拣存储
- 1秒变军装之faceswap
- 素数筛法
- 【Contest1025
- Curling 2.0
- HDU-6073 Matching In Multiplication
- Delphi实现DLL导出的Interface如何在其实语言中使用
- 排序-->归并排序
- 常见排序算法之JavaScript实现
- SQL Server-数据类型
- 第八章 BOM