POJ-2689 Prime Distance (两重筛素数,区间平移)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 13961 | Accepted: 3725 |
Description
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
Output
Sample Input
2 17
14 17
Sample Output
2,3 are closest, 7,11 are most distant.
There are no adjacent primes. 题目大意:给一个区间,找出这个区间内相邻的两个素数中差最大和最小的两个。
题目解析:两种方法,一种是 枚举 + Miller_Rabbin快速判定素数,另一种是将标记数组区间平移,两次筛素数。 第一种方法:
# include<iostream>
# include<cstdio>
# include<cstring>
# include<cstdlib>
# include<algorithm>
using namespace std;
# define ll long long
unsigned mypow(unsigned a,unsigned b,unsigned m)
{
if(b==)
return ;
if(b==)
return a%m;
ll temp=mypow(a,b/,m);
temp*=temp;
temp%=m;
if(b&)
temp*=a;
temp%=m;
return temp;
}
bool Miller_Rabbin(unsigned x)
{
if(x==)
return true;
for(int i=;i<=;++i){
unsigned a=rand()%(x-)+;
if(mypow(a,x-,x)!=)
return false;
}
return true;
}
int main()
{
unsigned a,b;
unsigned l1,l2,r1,r2,t;
int minn,maxn;
while(scanf("%u%u",&a,&b)!=EOF)
{
if(a==)
a=;
minn=;
maxn=;
l1=l2=r1=r2=a;
bool yy=true;
for(int i=a;i<=b;++i){
if(Miller_Rabbin(i)){
if(yy){
t=l1=r1=a;
yy=false;
}
else{
if(minn>i-t){
minn=i-t;
l1=t;
l2=i;
}
if(maxn<i-t){
maxn=i-t;
r1=t;
r2=i;
}
}
t=i;
}
}
if(maxn==){
printf("There are no adjacent primes.\n");
}else
printf("%u,%u are closest, %u,%u are most distant.\n",l1,l2,r1,r2);
}
return ;
}
这种暴力的方法效率不高,在UVa上取30个随机数能AC,在ZOJ上取20个随机数能AC,但在POJ上无论如何都AC不了。原因就是这三个OJ对时间的要求分别是3s,2s,1s。
下面是两重筛的实现。标记数组用的很灵活。
# include<iostream>
# include<cstdio>
# include<cmath>
# include<map>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std;
const int N=;
int pri[N],mark[],cnt;
vector<unsigned>v;
void init()
{
cnt=;
fill(mark,mark+N+,);
for(int i=;i<=N;++i){
if(mark[i])
pri[cnt++]=i;
for(int j=;j<cnt&&i*pri[j]<=N;++j){
mark[i*pri[j]]=;
if(i%pri[j]==)
break;
}
}
}
void work(unsigned a,unsigned b)
{
v.clear();
if(a==)
++a;
memset(mark,,sizeof(mark));
for(int i=;i<cnt;++i){
if(pri[i]>b)
break;
for(int c=a/pri[i];c*pri[i]<=b;++c){
if(c<=)
continue;
if(c*pri[i]<a)
continue;
mark[c*pri[i]-a]=;
}
}
for(int i=;i<=b-a;++i){
if(mark[i]==)
v.push_back(i+a);
}
}
void solve()
{
int l=v.size();
if(l<){
printf("There are no adjacent primes.\n");
return ;
}
int minn=<<,maxn=;
unsigned l1,l2,r1,r2;
for(int i=;i<l;++i){
if(minn>v[i]-v[i-]){
minn=v[i]-v[i-];
l1=v[i-];
l2=v[i];
}
if(maxn<v[i]-v[i-]){
maxn=v[i]-v[i-];
r1=v[i-];
r2=v[i];
}
}
printf("%u,%u are closest, %u,%u are most distant.\n",l1,l2,r1,r2);
}
int main()
{
init();
unsigned a,b;
while(scanf("%u%u",&a,&b)!=EOF)
{
work(a,b);
solve();
}
return ;
}
POJ-2689 Prime Distance (两重筛素数,区间平移)的更多相关文章
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 由于L<U<=2147483647,直接筛 ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- POJ - 2689 Prime Distance (区间筛)
题意:求[L,R]中差值最小和最大的相邻素数(区间长度不超过1e6). 由于非素数$n$必然能被一个不超过$\sqrt n$的素数筛掉,因此首先筛出$[1,\sqrt R]$中的全部素数,然后用这些素 ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- POJ 2689 Prime Distance (素数+两次筛选)
题目地址:http://poj.org/problem?id=2689 题意:给你一个不超过1000000的区间L-R,要你求出区间内相邻素数差的最大最小值,输出相邻素数. AC代码: #includ ...
随机推荐
- Chrome 性能监测
前端性能优化一直是前端工作中必不可少的一部分,但是我们如何知道哪些部分的性能有优化的空间呢?此时,Chrome 性能监测就派上用场了. 正所谓:知己知彼,百战百胜,只有确定了性能瓶颈,才能有条不紊地进 ...
- Python Web学习笔记之并发编程IO模型
了解新知识之前需要知道的一些知识 同步(synchronous):一个进程在执行某个任务时,另外一个进程必须等待其执行完毕,才能继续执行 #所谓同步,就是在发出一个功能调用时,在没有得到结果之前,该调 ...
- exp9《网络对抗》web安全基础实践201453331魏澍琛
201453331魏澍琛web安全基础实践 一.实验过程 1.webgoat开启 2.Injection Flaws练习 Command Injection 原网页中没有注入的地方,那就用burpsu ...
- 20135234mqy-——信息安全系统设计基础第二周学习总结
Linux基础 1.Linux命令 command [options] [arguments] //中括号代表是可选的,即有些命令不需要选项也不需要参数 选项(options)或参数(argument ...
- QT学习资源
http://www.qter.org/portal.php?mod=view&aid=26
- Spyder clear variable explorer from memory
https://stackoverflow.com/questions/45853595/spyder-clear-variable-explorer-along-with-variables-fro ...
- jquery 之 extend的实现
function getOpt(target, obj1, obj2, obj3){ $.extend(target, obj1, obj2, obj3); return target; } var ...
- AtCoder Tenka1 Programmer Beginner Contest 解题报告
赛时写了ABC,D实在没啥思路,然后C又难调...然后就从写完AB时的32名掉到了150+名 T_T 码力不够,思维不行,我还是AFO吧 比赛链接 A - Measure sb模拟,奇数串倒着输出偶数 ...
- [BZOJ1776][Usaco2010 Hol]cowpol 奶牛政坛
Description 农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片 ...
- Linux 安装 mysql 转 http://www.cnblogs.com/fnlingnzb-learner/p/5830622.html
到mysql官网下载mysql编译好的二进制安装包,在下载页面Select Platform:选项选择linux-generic,然后把页面拉到底部,64位系统下载Linux - Generic (g ...