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的更多相关文章

  1. Primes on Interval(二分 + 素数打表)

    Primes on Interval Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u ...

  2. HDU 5901 Count primes 论文题

    Count primes 题目连接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5901 Description Easy question! C ...

  3. hdu 5901 Count primes (meisell-Lehmer)

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  4. Codeforces Round #147 (Div. 2)

    A. Free Cash 判断值相同的最长长度. B. Young Table 按从上到下,从左到右排序,每个位置最多交换一次. C. Primes on Interval \(p_i\)表示位置\( ...

  5. 130712周赛(CF)

    这次练习从第一题开始注定水了,1A的题目wa了3次,第三题走进了错误的思想,wa到死....其他三个题目看都没看...........赛后慢慢搞. A. Free Cash 巨水的一题,直接找出每个时 ...

  6. codeforce --- 237C

    C. Primes on Interval time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  7. 13年7月13日CF练习 Codeforces Round #147 (Div. 2)

    这场div2可以说是我见过的比较水的一场吧.基本都是一眼题. 比赛地址http://acm.bnu.edu.cn/bnuoj/contest_show.php?cid=1836 题号是237A-237 ...

  8. Meissel Lehmer Algorithm 求前n个数中素数个数 【模板】

    Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  9. 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找

    C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...

随机推荐

  1. crontab 的例子

    0 5 * * * python /home/iscas/xiujinnews/wangyi.py >>/home/iscas/xiujinnews/l og/wangyi.log 2&g ...

  2. JAVA内容回顾(二)——面向对象(OOP)

    1.类与对象 类:类指的是同种对象的抽象,看不见摸不着的.包含有属性与方法. 对象:是类的具体实现,看的见摸得着的东西. 类是对象的抽象,对象是类的具体实现. 2.访问修饰符 public:在项目的任 ...

  3. python join()阻塞的用法

    join()阻塞的用法,用来检测线程有没有完全执行完毕 #!/usr/bin/env python#-*- coding:utf-8 -*-import threadingimport time de ...

  4. fidder https以及Fiddler抓取HTTPS协议

    一.浅谈HTTPS 我们都知道HTTP并非是安全传输,在HTTPS基础上使用SSL协议进行加密构成的HTTPS协议是相对安全的.目前越来越多的企业选择使用HTTPS协议与用户进行通信,如百度.谷歌等. ...

  5. 设计模式--装饰者设计模式(Decorator)

    装饰者模式又叫包装模式. 通过另一个对象来扩展自己的行为,在不破坏类的封装的情况下,实现松耦合,易扩展的效果.   抽象组件角色: 一个抽象接口,是被装饰类和装饰类的父接口可以给这些对象动态地添加职责 ...

  6. C#中的??是什么意思

    C#中的??是什么意思 DJ8Angus | 浏览 49982 次  2012-01-16 12:07 2012-01-16 12:23   最佳答案   如果不赋予初值,C#的变量是不允许直接使用的 ...

  7. JObject对json的操作

    一,需去程序集添加using Newtonsoft.Json.Linq;引用 using System; using System.Collections.Generic; using System. ...

  8. 本地Fiddler传递XML格式数据,调试微信功能。

    一,调试关注和取消关注 User-Agent: Fiddler Host: localhost:4561Content-Type: application/json; charset=utf-8 Co ...

  9. js返回上一页并刷新代码整理

    一:JS 重载页面,本地刷新,返回上一页 复制代码 代码如下: <a href="javascript:history.go(-1)">返回上一页</a> ...

  10. Storm中-Worker Executor Task的关系

    Storm在集群上运行一个Topology时,主要通过以下3个实体来完成Topology的执行工作:1. Worker(进程)2. Executor(线程)3. Task 下图简要描述了这3者之间的关 ...