一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子。

打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可。

Code:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int Range=50000;
const int N=1000000+233;
int f[N],vis[Range+233],prime[Range];
int cnt;
void get_prime(){
for(int i=2;i<=Range;++i){
if(!vis[i])prime[++cnt]=i;
for(int j=1;j<=cnt&&prime[j]*i<=Range;++j){
vis[prime[j]*i]=1;
if(i%prime[j]==0)break;
}
}
}
int main(){
get_prime();
int L,U;
while(scanf("%d%d",&L,&U)!=EOF){
memset(f,0,sizeof(f));
if(L==1)L=2;
for(int i=1;i<=cnt;++i){
int a=L%prime[i]==0?L/prime[i]:L/prime[i]+1;
int b=U/prime[i];
for(int j=a;j<=b;++j)if(j>1)f[j*prime[i]-L]=1;
}
int p=-1,x1,x2,maxans=-1,minans=N,y1,y2;
for(int i=0;i<=U-L;++i)
if(f[i]==0){
if(p==-1){p=i;continue;};
if(maxans<i-p){maxans=i-p,x1=p+L,x2=i+L;}
if(minans>i-p){minans=i-p,y1=p+L,y2=i+L;}
p=i;
}
if(maxans==-1)
cout<<"There are no adjacent primes."<<endl;
else cout<<y1<<","<<y2<<" are closest, "<<x1<<","<<x2<<" are most distant."<<endl;
}
return 0;
}

  

Prime Distance POJ - 2689 线性筛的更多相关文章

  1. Prime Distance POJ - 2689 (数学 素数)

    The branch of mathematics called number theory is about properties of numbers. One of the areas that ...

  2. Sum of Consecutive Prime Numbers POJ - 2739 线性欧拉筛(线性欧拉筛证明)

    题意:给一个数 可以写出多少种  连续素数的合 思路:直接线性筛 筛素数 暴力找就行   (素数到n/2就可以停下了,优化一个常数) 其中:线性筛的证明参考:https://blog.csdn.net ...

  3. POJ-2689 Prime Distance (两重筛素数,区间平移)

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13961   Accepted: 3725 D ...

  4. 题解报告:poj 2689 Prime Distance(区间素数筛)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  5. Prime Distance(二次筛素数)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  6. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  7. poj 2689 Prime Distance (素数二次筛法)

    2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...

  8. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

  9. 数论 - 素数的运用 --- poj 2689 : Prime Distance

    Prime Distance Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12512   Accepted: 3340 D ...

随机推荐

  1. 嵌入式 ThriftServer in Spark

    我们知道在Spark中可以通过start-thriftServer.sh 来启动ThriftServer,之后并可以通过beeline或者JDBC来连接并执行Spark SQL.在一般的Spark应用 ...

  2. There are no packages available for installation. Sublime3解决方法

    最近在学习Vue,在配置sublime3的时候,想要高亮vue的语法,下载点插件 Package Control的时候,总报  There are no packages available for ...

  3. luogu P4756 Added Sequence(凸包+思维)

    一眼望去不会. 考虑问题中的\(f(i,j)=|\sum_{p=i}^{j}​a_p​ |\)的实际意义. 其实就是前缀和相减的绝对值. \(f(i,j)=|\ sum[j]-sum[i-1]\ |\ ...

  4. HDU1867 - A + B for you again

    Generally speaking, there are a lot of problems about strings processing. Now you encounter another ...

  5. [tyvj2054] 四叶草魔杖 (最小生成树 状压dp)

    传送门 Background 陶醉在彩虹光芒笼罩的美景之中,探险队员们不知不觉已经穿过了七色虹,到达了目的地,面前出现了一座城堡和小溪田园,城堡前的木牌上写着"Poetic Island&q ...

  6. 【LibreOJ 6279】 数列分块入门 3 (分块)

    传送门 code: //By Menteur_Hxy #include<cstdio> #include<iostream> #include<algorithm> ...

  7. P1828 香甜的黄油 Sweet Butter (spfa)

    题目描述 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  8. 设置ssh会话过期时间

    客户端配置选项: 编辑配置文件 ~/.ssh/config 加入以下配置项,如不存在新建一个即可: Host * ServerAliveInterval 300 ServerAliveCountMax ...

  9. JAVAEE网上商城项目总结

    发送邮件实现(使用QQ邮箱发送到指定邮箱) 需要的jar 邮件发送类代码: package util; import java.util.Properties; import javax.mail.A ...

  10. HDU 2138

    这题用MILLER测试应该是不可避免的. #include <iostream> #include <cstdio> #include <stdlib.h> #in ...