Primes on Interval
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 1001000;
#define inf (1<<29)
//上面的位运算还真心没有看懂
// p[i] is i-th prime's position
bool pp[maxn]; //里面保存的是一个素数的信息
int p[maxn] , cnt = 0; //将素数保留在数组中间
int ss[maxn] , tt[maxn];//在这里申请了这么多的数组我就是没有看懂了是到底为啥
void init() {
cnt = 0;
pp[0] = pp[1] = 1;//前两个数字都是不予考虑的
tt[0] = tt[1] = -1;
for(int i=2;i<maxn;i++) {
if(!pp[i]) {
p[cnt++] = i; //这个是将素数保留在表格中间吗?
for(int j=i+i;j<maxn;j+=i) {
pp[j] = true; //这个是素数达标
}
}
tt[i] = cnt - 1;
}
for(int i=0;i<maxn;i++) {
if(!pp[i]) ss[i] = tt[i];
else ss[i] = tt[i] + 1;
}
}
int main() {
init();
int a , b , k;
while(~scanf("%d%d%d" , &a,&b,&k)) {
int s = ss[a] , t = tt[b];
int num = t - s + 1;
if(num < k) {//先判断在这个区间之中里面的素数量是否达到了题目的要求,否则直//接退出
printf("-1\n");
continue;
}
int ans = 0;
int tmp;
tmp = b - p[t-k+1] + 1;
if(tmp > ans) ans = tmp;
tmp = p[s+k-1] - a + 1;
if(tmp > ans) ans = tmp;
for(int i=s;i+k<=t;i++) {
tmp = p[i+k] - p[i];
if(tmp > ans) ans = tmp;
}
printf("%d\n" , ans);
}
return 0;
}
//本题的主要思路是通过打表,成功后就可以比较简单的得到结果
Primes on Interval的更多相关文章
- Primes on Interval(二分 + 素数打表)
Primes on Interval Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u ...
- HDU 5901 Count primes 论文题
Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...
- hdu 5901 Count primes (meisell-Lehmer)
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- Codeforces Round #147 (Div. 2)
A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...
- 130712周赛(CF)
这次练习从第一题开始注定水了,1A的题目wa了3次,第三题走进了错误的思想,wa到死....其他三个题目看都没看...........赛后慢慢搞. A. Free Cash 巨水的一题,直接找出每个时 ...
- codeforce --- 237C
C. Primes on Interval time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 13年7月13日CF练习 Codeforces Round #147 (Div. 2)
这场div2可以说是我见过的比较水的一场吧.基本都是一眼题. 比赛地址http://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1836 题号是237A-237 ...
- Meissel Lehmer Algorithm 求前n个数中素数个数 【模板】
Count primes Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
随机推荐
- eclipse 发布web工程,修改tomcat端口
如果想修改tomcat发布的端口,有两种方法: 1.用记事本打开tomcat安装目录下的conf文件夹下的server.xml,找到<Connector port="8080" ...
- Python基础篇-day8
本节目录1.抽象接口2.静态方法.类方法.属性方法3.类的特殊方法 3.1 __doc__ 表示类的描述信息(注释) 3.2 __module__ 和 __class__ 3.3 __init__ 构 ...
- ServiceContract,OperationContract
代码如下 [ServiceContract] //服务协定定义 using System.ServiceModel; public interface IInterface1 { [Operation ...
- 设置phpMyAdmin本地自动登陆
一般配置本地测试用的 phpMyAdmin 可以不用每次输入帐号密码,打开后自动登陆就行了. 版本: phpMyAdmin 3.5.3 打开: phpMyAdmin 根目录 复制: config.sa ...
- scala 数组 基本类型
变量尽量用valvar 是不可变 final 常用的 Int̵ Double̵ Long̵ String没有基本类型.scala 任何对象都继承Any Int Double 继承AnyVal Stri ...
- linux环境vnc部署过程详解
vnc服务端机器地址:10.165.38.68 vnc客户端机器地址:本机(windows机器) vnc客户端包:vnc_82537_82537.rar (百度云盘下载地址:http://pan.ba ...
- 超好:web app变革之rem
感谢你的阅读,本文由 腾讯ISUX 版权所有,转载时请注明出处,违者必究,谢谢你的合作.注明出处格式:腾讯ISUX (https://isux.tencent.com/web-app-rem.html ...
- Eclipse使用Maven tomcat:run命令启动web项目
Eclipse安装好m2e插件,使用Maven构建项目后,启动web项目就行就非常简单了,你不再需要下载然后在eclipse中配置tomcat. 右键你的项目 -> Run As -> R ...
- HDU 2181 哈密顿绕行世界问题
Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...
- JS复习:二十一章
一.XHR对象 Ajax( )对象的核心技术就是XMLHttpRequest对象. 二.XHR的用法 在使用XHR对象时,要调用的第一个方法是open( ),它接受3个参数:要发送的请求类型(&quo ...