poj 2689 Prime Distance(大区间筛素数)
http://poj.org/problem?id=2689
题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对。
由于L<U<=2147483647,直接筛素数是不行的,数组就开不了。可是能够依据素数筛的原理。我们先筛出sqrt(2147483647)以内的素数,然后拿这些素数去筛[L,U]之间的素数,即两次素数筛。可是L,U还是非常大,但U-L<=1000000,所以进行区间平移,将[L,U]平移为[0,U-L],就能用数组放得下。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std; const int maxn = 47000;
int prime[maxn];
bool flag[maxn];
bool a[1000010];
LL L,U; void get_prime()
{
memset(flag,true,sizeof(flag));
prime[0] = 0;
flag[0] = flag[1] = false;
for(int i = 2; i < maxn; i++)
{
if(flag[i])
prime[++prime[0]] = i;
for(int j = 1; j <= prime[0]&&prime[j]*i < maxn; j++)
{
flag[prime[j]*i] = false;
if(i%prime[j] == 0)
break;
}
}
}
//用已筛选的素数去筛[L,U]之间的素数,区间平移为[0,U-L];
void solve(LL L, LL u)
{
memset(a,true,sizeof(a));
if(L == 1)//特殊推断L为1的时候,映射为0,但1不是素数,所以a[0]为false。
a[0] = false;
for(int i = 1; i <= prime[0]; i++)
{
if(prime[i]*prime[i] > U)
break;
int l = L/prime[i];
if(L%prime[i]!=0)
l++;
if(l == 1) l = 2; // l=1时,L为素数,不能把L本身筛掉
int u = U/prime[i]; for(int j = l; j <= u; j++)
{
LL t = (long long)j*prime[i];
a[t-L] = false;
}
}
} int main()
{
get_prime();
while(~scanf("%lld %lld",&L,&U))
{
solve(L,U);
int Min = 1000010,Max = -1;
int ans[4];
int x,y;
x = y = -1;
for(int i = 0; i <= U-L; i++)
{
if(a[i] == true)
{
x = y;
y = i; if(x == -1)
continue;
int t = y-x;
if(t < Min)
{
ans[0] = x;
ans[1] = y;
Min = t;
}
if(t > Max)
{
ans[2] = x;
ans[3] = y;
Max = t;
}
}
}
if(x != -1 && y != -1)
{
printf("%lld,%lld are closest, %lld,%lld are most distant.\n",ans[0]+L,ans[1]+L,ans[2]+L,ans[3]+L);
}
else
printf("There are no adjacent primes.\n");
}
return 0;
}
poj 2689 Prime Distance(大区间筛素数)的更多相关文章
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- poj2689Prime Distance(大区间筛素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19635 Accepted: 5273 D ...
- 题解报告:poj 2689 Prime Distance(区间素数筛)
Description The branch of mathematics called number theory is about properties of numbers. One of th ...
- poj 2689 Prime Distance (素数二次筛法)
2689 -- Prime Distance 没怎么研究过数论,还是今天才知道有素数二次筛法这样的东西. 题意是,要求求出给定区间内相邻两个素数的最大和最小差. 二次筛法的意思其实就是先将1~sqrt ...
- [ACM] POJ 2689 Prime Distance (筛选范围大素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12811 Accepted: 3420 D ...
- 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 Time Limit: 1000MS Memory Limit: 65536K Description The branch o ...
- 数论 - 素数的运用 --- poj 2689 : Prime Distance
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12512 Accepted: 3340 D ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
随机推荐
- 如果把父组件的数据实时的传递到子组件:用watch
1.在子组件使用watch来监听传递给子组件的数据,然后更新子组件的数据. 2.watch和computed结合使用效果非常好. 参考链接:https://blog.csdn.net/zhouweix ...
- 常见c#正则表达式类学习整理
1.MatchCollection类 用于输入字符串所找到的成功匹配的集合,Regex.Matches 方法返回 MatchCollection 对象 用法 //str:要搜索匹配项的字符串 patt ...
- 用PHP 去掉所有html标签里的部分属性
用PHP 去掉所有html标签里的部分属性 http://zhidao.baidu.com/question/418471924.html 用PHP 去掉所有html标签里的部分属性 tppabs & ...
- iOS中 学会如何对sqlite3 进行封装
#waring ---(看官注意) ---使用说明: ①在创建自定义model类之前让该类继承自文件中的Model类, ②为model类选一个NSString属性作为主键:(既,在初始化方法里面将从父 ...
- 软件——protel 的pcb电路图制作
近期一直在学习PCB板的绘制.
- react取消监听scroll事件
如果要移除事件addEventListener的执行函数必须使用外部函数而不能直接使用匿名函数 错误写法: // 这样写是移除不了滚动事件的 componentDidMount() { // 添加滚动 ...
- 设置Maven默认的JDK为1.7,解决Update Maven Project默认为1.5和Maven打包报错2个问题
1.之前,一直遇到这个问题. Update Maven Project的时候,JDK变成了1.5的. 如果项目中有使用"@overdide",程序就会报错,需要手动修改JRE ...
- 如何将String类型转换成任意基本类型
[原创][C#] 如何将String类型转换成任意基本类型 Posted on 2009-12-02 09:47 YCOE 阅读( 2843) 评论( 14) 编辑 收藏 前几天,在写一个自动 ...
- 00087_File
1.IO概述 (1)要把数据持久化存储,就需要把内存中的数据存储到内存以外的其他持久化设备(硬盘.光盘.U盘等)上: (2)当需要把内存中的数据存储到持久化设备上这个动作称为输出(写)Output操作 ...
- [Firebase] Firebase Cloud Functions
Firebase cloud functions is similar to AWS lambda or serverless. You can deploy you code which wrote ...