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. zTree 勾选checkbox

    zTree 勾选checkbox var setting = {    check: {        enable: true//        chkboxType : { "Y&quo ...

  2. 显示ubuntu 10.4右上角网络图标

    在面板右击“添加到面板”,选择“通知区域”

  3. Journey

    Journey 题目链接:http://codeforces.com/problemset/problem/721/C dp/记忆化搜索/拓扑排序 刚开始想到用bfs+dp,fst(然而中间有一步逻辑 ...

  4. Ubuntur软件安装

    Ubuntu软件安装 Falsh sudo apt-get install flashplugin-installer

  5. asp.net 批量删除

    直接上代码: 1.页面部分 <script type="text/javascript" src="http://code.jquery.com/jquery-1. ...

  6. IOS 类的属性修饰符atomic

    在声明一个类的属性时,默认这个属性会被修饰atomic,意思是原子性访问的. nonatomic和atomic修饰的属性,在自己没有重写setter和getter的时候才会发生作用,其主要的作用可以理 ...

  7. HDU 1081 To The Max(动态规划)

    题目链接 Problem Description Given a two-dimensional array of positive and negative integers, a sub-rect ...

  8. LeetCode OJ 229. Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  9. CodeForces 697B Barnicle 模拟

    强行模拟 纪念一下…… #include<stdio.h> #include<iostream> #include<algorithm> #include<m ...

  10. python--sum函数--sum(axis=1)

    平时用的sum应该是默认的axis=0 就是普通的相加,当加入axis=1以后就是将一个矩阵的每一行向量相加. 例如: >>>import numpy as np >>& ...