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 ...
随机推荐
- Secure CRT 自动记录日志log配置
SecureCRT8.0的下载地址下载地址: 链接:https://pan.baidu.com/s/1i5q09qH 密码:4pa2 配置自动log操作如下: 1.options ---> Se ...
- MySQL数据库----完整性约束
一.介绍 约束条件与数据类型的宽度一样,都是可选参数 作用:用于保证数据的完整性和一致性主要分为: PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 FOREIGN KEY ...
- 如何使用python来对二维数组进行排序
1.复合排序 直接用numpy的lexsort就可以 import numpy as np data = np.array([[1,2,3,4,5], [1,2,3,6,7], [2,3,4,5,7] ...
- Python3基础 if else 格式 输入一个整数并判断是8吗
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 《js高级程序设计》--第三章数据类型
一.关键字 二.保留字 三.数据类型 (数据类型具有动态性) 1.Undefined 声明变量却未对其加以初始化(赋值) 2.Null null值表示一个空对象指针,而这也正是使用typeof操作 ...
- windows10下cygwin安装神器apt-cyg
一.背景 需要在cygwin下安装一些库 二.安装 2.1获取apt-cyg源码 git clone https://github.com/transcode-open/apt-cyg.git 2.2 ...
- Specify Computed Columns in a Table
https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table ...
- BZOJ2662: [BeiJing wc2012]冻结 spfa+分层图
Description “我要成为魔法少女!” “那么,以灵魂为代价,你希望得到什么?” “我要将有关魔法和奇迹的一切,封印于卡片之中„„” 在这个愿望被实现以后的世界里,人们享 ...
- Unity3D学习笔记(十六):Animator新动画
新动画系统: 给模型选择动画类型 普通动画:Generic 人形动画:Humanoid 建立动画控制器 - 在Project右击 - 选择Create-AnimatorContorller 将对应动画 ...
- C#学习笔记(十六):索引器和重载运算符
二维数组如何映射到一维数组 重载运算符 1.算术运算符 2.关系运算符, < 和 > 成对重载 using System; using System.Collections.Generic ...