Primes on Interval

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

Appoint description: 
System Crawler  (2016-04-26)

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 integer x(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
题意:
求最小的l使 x, x + 1, ..., x + l - 1 there are at least k prime numbers;
简单二分;
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int MAXN = 1e6 + ;
int dp[MAXN];
int vis[MAXN];
void db(){
memset(vis, , sizeof(vis));
vis[] = ;
for(int i = ; i <= sqrt(MAXN); i++){
if(!vis[i]){
for(int j = i * i; j < MAXN; j += i){
vis[j] = ;
}
}
}
dp[] = ;
for(int i = ; i < MAXN; i++){
dp[i] = dp[i - ];
if(!vis[i])dp[i]++;
}
}
bool js(int l, int a, int b, int k){
for(int i = a; i <= b - l + ; i++){
if(dp[i + l - ] - dp[i - ] < k)return false;
}
return true;
}
int erfen(int l, int r, int a, int b, int k){
int mid, ans = -;
while(l <= r){
mid = (l + r) >> ;
if(js(mid, a, b, k)){
ans = mid;
r = mid - ;
}
else l = mid + ;
}
return ans;
}
int main(){
db();
int a, b, k;
while(~scanf("%d%d%d", &a, &b, &k)){
printf("%d\n", erfen(, b - a + , a, b, k));
}
return ;
}

Primes on Interval(二分 + 素数打表)的更多相关文章

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

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

  2. Codeforces Round #315 (Div. 2C) 568A Primes or Palindromes? 素数打表+暴力

    题目:Click here 题意:π(n)表示不大于n的素数个数,rub(n)表示不大于n的回文数个数,求最大n,满足π(n) ≤ A·rub(n).A=p/q; 分析:由于这个题A是给定范围的,所以 ...

  3. Fermat’s Chirstmas Theorem (素数打表的)

                                                                             Fermat’s Chirstmas Theorem ...

  4. CodeForces 385C Bear and Prime Numbers 素数打表

    第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...

  5. [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

  6. Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)

    题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...

  7. Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)

    题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...

  8. hdoj--5104--Primes Problem(素数打表)

    Primes Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  9. hdu 5104 素数打表水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...

随机推荐

  1. python高级编程之选择好名称:pepe8和命名最佳实践

    # # -*- coding: utf-8 -*- # # python:2.x # __author__ = 'Administrator' # my_list=['a','b','c','d'] ...

  2. pyqt QTimer,QThread例子学习

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' from PyQt4.QtGui import * from PyQ ...

  3. copy模块

    # -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #copy复制对象 #作用:提供一些函数,可以使用潜复制和深复制进行 ...

  4. 分享一个3D球面标签云

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. POJ 3254 炮兵阵地(状态压缩DP)

    题意:由方格组成的矩阵,每个方格可以放大炮用P表示,不可以放大炮用H表示,求放最多的大炮,大炮与大炮间不会互相攻击.大炮的攻击范围为两个方格. 分析:这次当前行的状态不仅和上一行有关,还和上上行有关, ...

  6. Unable to run mksdcard SDK tool.

    Ubuntu 14.04,安装android studio后运行出错,sdk manager不能正常运行 Unable to run mksdcard SDK tool. 原因,缺少运行需要的库:li ...

  7. EffectiveC#2--为你的常量选择readonly而不是const

    1.对于常量,C#里有两个不同的版本: 编译时常量--效率相比更高些,但可维护性不好,保留的目的是为了性能.const关键字申明 public const int _Millennium = 2000 ...

  8. MBTI性格测试

    INFP 哲学家型——生活在自己的理想世界 报告接收人: 才储成员4361454 日期: 2014/9/2 一.你的MBTI图形 倾向示意图表示四个维度分别的倾向程度.从中间往两侧看,绿色指示条对应下 ...

  9. 我的第一个MVC项目

    这是本人第一个完全按照MVC开发模式完成的项目,非常简陋,仅仅完成的查询,保存,删除基本功能,而且存在编码问题没有解决,以后会慢慢增加功能.慢慢改进 package com.sdjt.dao; imp ...

  10. c++ 指针的简单用法

    对于指针,其实只需要明白几点就可以. 1.指针,是一个数值为地址的变量,这里尤其注意,指针变量的值是地址!就是例如40002这种像门牌号的地址值,其实就是内存中的一个编号. 2.&,该符号的意 ...