题目:

Description

You've decided to carry out a survey in the theory of prime numbers. Let us remind you that a prime number is a positive integer that has exactly two distinct positive integer divisors.

Consider positive integers aa + 1, ..., b (a ≤ b). You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such that for any integerx (a ≤ x ≤ b - l + 1) among l integers xx + 1, ..., x + l - 1 there are at least k prime numbers.

Find and print the required minimum l. If no value l meets the described limitations, print -1.

Input

A single line contains three space-separated integers a, b, k (1 ≤ a, b, k ≤ 106a ≤ b).

Output

In a single line print a single integer — the required minimum l. If there's no solution, print -1.

Sample Input

Input
2 4 2
Output
3
Input
6 13 1
Output
4
Input
1 4 3
Output
-1

代码:

#include <cstdio>
#include <cstring>
const int maxn = 1000010;
int sum[maxn],a,b,k;
bool pri[maxn];
void init(){
for(int i = 2;i < maxn;i++){
sum[i] = sum[i-1];
if(pri[i]) continue;
sum[i]++;
for(int j = i+i;j < maxn;j += i)
pri[j] = 1;
}
}

bool check(int mid){
for(int i = a;i <= b-mid+1;i++){
if(sum[i+mid-1]-sum[i-1] < k)
return 0;
//如果是这种情况说明结果是一定不会满足条件的,我们应该重新确定一个更大的min值来进行判断
}
return 1;
}

int main(){
init();
scanf("%d%d%d",&a,&b,&k);
if(sum[b]-sum[a-1] < k){
printf("-1\n");
return 0;

//当所给的区间范围内都没有满足条件的素数个数的时候,结果可以直接的返回
//但是如果满足了这个条件的情况下,l是一定会有解的
int l = 1,r = b-a+1,ans;
while(l <= r){
int mid = (l+r)>>1;
if(check(mid)) ans = mid,r = mid-1;
else l = mid+1;
}
printf("%d\n",ans);
}

Codeforces 237C的更多相关文章

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

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

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

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

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. 阿里云 CentOS7.2 配置FTP+Node.js环境

    本人小白,写下这篇博客意在记录踩过的坑,大神请绕道~ 准备工作 安装自己喜欢的连接软件(一般是putty或者xshell),本人选择的是xshell,软件如图 : 通过软件中的ssh连接连接上已经购买 ...

  2. 32bit程序在64bit操作系统下处理重定向细节(转自http://bbs.pediy.com/showthread.php?t=89054)

    1. 64bit操作系统的重定向机制以及目的 在64bit操作系统中,为了无缝兼容32bit程序的运行,64bit的Windows操作系统采用重定向机制.目的是为了能让32bit程序在64bit的操作 ...

  3. 最直接的教你OC中Block的简单使用场景

    场景一: A控制器跳转到B控制器   --   B控制器事件处理通过Block回调给A控制器 A 跳转前界面如下 点击ToB按钮到控制器B 在控制器B中点击按钮返回到A界面如下             ...

  4. python+selenium+Eclipse安装

    1.安装python 参考安装python:http://www.cnblogs.com/beyongblue/p/4215740.html 2.安装python管理工具setuptools 3.安装 ...

  5. 新手站长选择WordPress程序建站需要注意的8个问题

    文章出自:http://www.banwagongvps.com/119.html 如今我们不论是出于个人的兴趣爱好,还是出于我们希望通过搭建自己的网站获利的动机,入门级别的都变得非 常的简单,我们只 ...

  6. FZU 1502 Letter Deletion(DP)

    Description You are given two words (each word consists of upper-case English letters). Try to delet ...

  7. 2016年团体程序设计天梯赛-决赛 L2-3. 互评成绩(25)

    学生互评作业的简单规则是这样定的:每个人的作业会被k个同学评审,得到k个成绩.系统需要去掉一个最高分和一个最低分,将剩下的分数取平均,就得到这个学生的最后成绩.本题就要求你编写这个互评系统的算分模块. ...

  8. (负)-margin在页面布局中的应用

    有关margin的原理可以看看这篇文章:http://www.cnblogs.com/2050/archive/2012/08/13/2636467.html#2457812 有关margin负值的几 ...

  9. web.py 学习(二)Worker

    Rocket Server 启动一个线程监听客户端的连接,收到连接将连接放置到队列中.线程池中的Worker会以这个连接进行初始化.Rocket中Worker的基类是: class Worker(Th ...

  10. Objetive-C +load方法研究

    load方法的执行时机           Objetive-C 的runtime会在一个类的所有方法加载到内存中时调用这个类的+load() 方法,因为每个类的方法只是加载一次,所以每个+load( ...