poj Prime Distance 2689 (素数二次打表找最近与最远素数) 好题
2015-11-10 22:22
513 查看
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).
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
Sample Output
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14881 | Accepted: 3948 |
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.
//看大神的博客的,感觉好机智
#include<stdio.h> #include<string.h> #include<algorithm> #define N 1000010 #define ll long long #define INF 0x3f3f3f3f using namespace std; int v ; ll p ; ll pp ; ll k,l,u; void getp() { memset(v,0,sizeof(v)); v[1]=1; k=0; for(ll i=2;i<=50012;i++) { if(!v[i]) { p[++k]=i; for(ll j=i*i;j<50001;j+=i) v[j]=1; } } } void getpp() { ll t; memset(v,0,sizeof(v)); for(ll i=1;i<=k;i++) { t=l/p[i]; while(t*p[i]<l||t<=1) t++; for(ll j=t*p[i];j<=u;j+=p[i]) { if(j>=l) v[j-l]=1; } } if(l==1) v[0]=1; } int main() { getp(); ll i; while(scanf("%lld%lld",&l,&u)!=EOF) { getpp(); ll cnt=0; for(i=0;i<=u-l;i++) { if(!v[i]) pp[++cnt]=i+l; } if(cnt<=1) { printf("There are no adjacent primes.\n"); continue; } ll min=INF,max=-INF; ll aa,bb,cc,dd; for(i=1;i<cnt;i++) { if(pp[i+1]-pp[i]<min) { min=pp[i+1]-pp[i]; aa=pp[i]; bb=pp[i+1]; } if(pp[i+1]-pp[i]>max) { max=pp[i+1]-pp[i]; cc=pp[i]; dd=pp[i+1]; } } printf("%lld,%lld are closest, %lld,%lld are most distant.\n",aa,bb,cc,dd); } return 0; }
相关文章推荐
- Kernel density estimation 核密度估计
- MongoDb 安装服务 以及 安全配置
- OnTouchEvent方法:简单应用
- 使用javah生成.h文件, 出现无法访问android.app,Activity的错误的解决
- C语言网络编程(三)建立套接字通讯TCP
- 搭建CAS单点登录服务器
- 分治法之归并排序
- [TwistedFate]UIView UILabel
- ASI与AFNetworking对区别
- intent.addFlag()的Flag的类型介绍
- WebApi系列~按需序列化字段~Hot
- [安卓] 16、ListView和GridView结合显示单元实现自定义列表显示效果
- 物联网与嵌入式系统概论-week1-What Is the IoT- Lesson2: Trends of the IoT
- java笔记-继承(Extends)
- 运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try aga
- UI学习第一天UIView 和UILabel
- 基本类型的hashCode重写方案
- 微众银行面试小总结
- lamp 环境搭建遇到的问题
- PHP常量