[题解](区间质数筛)POJ_2689 Prime Distance
区间筛素数:先筛出1~sqrt(R)的素数,然后对于每个询问只要用这些素数筛掉区间内的合数即可。
几个细节:
1.特判和1有关的一些情况
2.每次减去L偏移量,数组只开区间大小
3.POJ无法使用万能头文件(需要火星救援(大雾
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int prime[];
bool ck[],tmp[];
int l,r,tot;
int main(){
for(int i=;i<=;i++){
if(!ck[i])prime[++tot]=i;
for(int j=;j<tot;j++){
if(i*prime[j]>)break;
ck[i*prime[j]]=;
if(i%prime[j]==)break;
}
}
while(scanf("%d%d",&l,&r)!=EOF){
if(l==)l=;//特判
memset(tmp,,sizeof(tmp));
for(int i=;i<=tot;i++){
int a=(l-)/prime[i]+;
int b=r/prime[i];
a=max(,a);
for(int j=a;j<=b;j++)
tmp[j*prime[i]-l]=;//减去l映射
}
int lst=-,minans=0x7fffffff,maxans=,xa,ya,xb,yb;
for(int i=;i<=r-l;i++){
if(!tmp[i]){
if(lst==-){lst=i;continue;}
if(maxans<i-lst)
maxans=i-lst,xa=lst+l,ya=i+l;
if(minans>i-lst)
minans=i-lst,xb=lst+l,yb=i+l;
lst=i;
}
}
if(maxans==)printf("There are no adjacent primes.\n");
else printf("%d,%d are closest, %d,%d are most distant.\n",xb,yb,xa,ya);
}
}
[题解](区间质数筛)POJ_2689 Prime Distance的更多相关文章
- 题解报告: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: 13961 Accepted: 3725 D ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- 【题解】UVA10140 [Prime Distance]
[题解]UVA10140 Prime Distance 哈哈哈哈\(miller-rabbin\)水过去了哈哈哈 还能怎么办呢?\(miller-rabbin\)直接搞.枚举即可,还跑得飞快. 当然此 ...
- POJ-2689 Prime Distance,区间素数筛法
Prime Distance 只会埃氏筛法的弱鸡今天读了读挑战程序设计120页,明白了求小区间内素数的方 ...
- [POJ268] Prime Distance(素数筛)
/* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...
- 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]
题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...
- poj 2689 Prime Distance(区间筛选素数)
Prime Distance Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9944 Accepted: 2677 De ...
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
Prime Distance Time Limit: 2 Seconds Memory Limit: 65536 KB The branch of mathematics called nu ...
随机推荐
- 有关Option.inSamplSize 和 Compress 图片压缩
1.图片占用内存:占用的内存 = 图片长度 * 图片宽度 * 单位像素占用的字节数 注: 图片长度和图片宽度就是图片在行列上的像素数量. 图片格式: ALPHA_8:表示8位Alpha位图,即A=8, ...
- iOS 第三方登录之 QQ登录
一. 首先需要下载腾讯qq登录所需的库,下载地址是http://open.qq.com/ . 需要用到的有TencentOpenAPI.framework 和TencentOpenApi_IOS_Bu ...
- h5 placeholder 设置无效
下面设置方式无效: ::-webkit-input-placeholder { /* WebKit browsers */ color: #999; } :-moz-placeholder { /* ...
- linux vsftpd
FTP是CS架构的.使用的是ftp协议. root@ubuntu:/# apt install vsftpd root@ubuntu:/# service vsftpd status ● vsftpd ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- IOS开发学习笔记(1)-----UILabel 详解
1. [代码][C/C++]代码 //创建uilabelUILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, ...
- Python使用multiprocessing实现一个最简单的分布式作业调度系统
Python使用multiprocessing实现一个最简单的分布式作业调度系统介绍Python的multiprocessing模块不但支持多进程,其中managers子模块还支持把多进程分布到多台机 ...
- OpenCV在Zedboard上的移植
OpenCV编译 本文的前提是zynq交叉编译环境设置 下载opencv3.1.0源码,并解压 wget https://github.com/Itseez/opencv/archive/3.1.0. ...
- 22.java方法的定义
java中的方法:就相当于c语言中的函数:sun在开发java的时候,为提高其代码的重复利用率,引入了方法. 什么是方法? 方法就是一段代码片段,这个片段可以完成特定的功能,并且可以重复利用. 从入口 ...
- CS231n 2016 通关 第四章-NN 作业
cell 1 显示设置初始化 # A bit of setup import numpy as np import matplotlib.pyplot as plt from cs231n.class ...