POJ 2689-Prime Distance(区间素数)
2016-06-02 19:26
495 查看
Prime Distance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 15901 | Accepted: 4227 |
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.
Source
Waterloo local 1998.10.17
题目意思:
求给定两个数区间内的所有素数中,相邻两个相差值最大和最小的数并输出;如果没有就输出There are no adjacent primes.
解题思路:
打素数表筛选出两个数区间内的所有素数,再两两比较求出最值。#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1000005
#define INF 0xfffffff
using namespace std;
typedef long long ll;
bool is_prime[MAXN];
bool is_prime_small[MAXN];
ll a,b;
void segment_sieve()//筛选a~b区间内的素数,is_prime[i-a]=true时,i是素数
{
for(ll i=0; i*i<=b; ++i)
is_prime_small[i]=true;
for(ll i=0; i<=b-a; ++i)
is_prime[i]=true;
for(ll i=2; i*i<=b; ++i)
{
if(is_prime_small[i])
{
for(ll j=2*i; j*j<=b; j+=i)
is_prime_small[j]=false;//2~sqrt(b)
for(ll j=max(2LL,(a+i-1)/i)*i; j<=b; j+=i)
is_prime[j-a]=false;//a~b
}
}
}
ll cnt,prime[MAXN];//a~b区间内素数的个数和素数表
void getnum()
{
for(ll i=a; i<=b; ++i)
if(is_prime[i-a])
if(i!=1)
prime[cnt++]=i;
//for(ll i=0; i<=cnt; ++i)
//cout<<prime[i]<<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
while(cin>>a>>b)
{
memset(is_prime,false,sizeof(is_prime));
memset(is_prime_small,false,sizeof(is_prime_small));
memset(prime,0,sizeof(prime));
cnt=0;
segment_sieve();
getnum();
ll x=0,xx=0,y=0,yy=0,Max=-1,Min=INF;
for(ll i=1; i<cnt; ++i)
{
if((prime[i]-prime[i-1])<Min)
{
x=prime[i-1],xx=prime[i];
Min=xx-x;
}
if((prime[i]-prime[i-1])>Max)
{
y=prime[i-1],yy=prime[i];
Max=yy-y;
}
}
if(cnt==0||cnt==1)
cout<<"There are no adjacent primes."<<endl;
else
cout<<x<<","<<xx<<" are closest, "<<y<<","<<yy<<" are most distant."<<endl;
}
return 0;
}
/*
1 5
2 17 14 1722 37
22801763 22801787
2147483000 2147483647
*/
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性