题目:

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. 移动端rem适配问题

    将下面这段代码,放在头部的script标签里,可解决字体适配问题 比例是28px = 1rem function __setFontSize__(){document.documentElement. ...

  2. PLSQL 几种游标的用法

    分类: Oracle 1. PL/SQL里的游标可以分为显式和隐式两种,而隐式有分为select into隐式游标和for .. in 隐式游标两种.所以,我们可以认为,有3种游标用法: A. 显式游 ...

  3. javascript 延时执行函数

    延时执行函数,貌似有些多此一举, 也许还是有点用 记在这儿 var test = { delay : function(lifetime){ var data; setTimeout(function ...

  4. C#Stopwatch的使用,性能测试

    一,先开启开始或继续测量某个时间间隔的运行时间,然后停止,最后重置时间,输出. using System; using System.Collections.Generic; using System ...

  5. apache动态添加模块

    Apache已经安装完毕并投入运行,但是后来却发现部分模块没有加载,当然有两个方法: 1. 一是完全重新编译Apache, 再安装 2. 编译模块为SO文件,使用LoadModule指令加载扩展模块. ...

  6. 一行一行分析JQ源码学习笔记-04

    jquery添加方法和属性 jquery.fn=jquery.prototype ={ jquery版本 } construtor  指向修正 js源码中 fun  aaa(){} 会自动生成一句   ...

  7. Python之深浅拷贝&函数

    一.深浅拷贝 深浅拷贝是指copy模块下的copy()和deepcopy()方法. 1.浅拷贝 示例: >>> import copy >>> a = 'hello ...

  8. openwrt 更改 debug 等级(hostapd)

    https://wiki.openwrt.org/doc/devel/debugging 调试hostapd,其中hostapd的调试等级如下: # Levels (minimum value for ...

  9. tar的打包-压缩与解压缩,并解压到指定的目录

    tar在linux上是常用的打包.压缩.加压缩工具,他的参数很多,折里仅仅列举常用的压缩与解压缩参数 参数: -c :create 建立压缩档案的参数:-x : 解压缩压缩档案的参数:-z : 是否需 ...

  10. HDU 1258 Sum It Up(DFS)

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...