Primes on Interval(二分 + 素数打表)
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
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 a, a + 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 x, x + 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 ≤ 106; a ≤ b).
Output
In a single line print a single integer — the required minimum l. If there's no solution, print -1.
Sample Input
2 4 2
3
6 13 1
4
1 4 3
-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(二分 + 素数打表)的更多相关文章
- 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]-CodeForces 237C,素数打表,二分查找
C. Primes on Interval time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 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是给定范围的,所以 ...
- Fermat’s Chirstmas Theorem (素数打表的)
Fermat’s Chirstmas Theorem ...
- CodeForces 385C Bear and Prime Numbers 素数打表
第一眼看这道题目的时候觉得可能会很难也看不太懂,但是看了给出的Hint之后思路就十分清晰了 Consider the first sample. Overall, the first sample h ...
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11978 A ...
- Aladdin and the Flying Carpet LightOJ - 1341 (素数打表 + 算术基本定理)
题意: 就是求a的因数中大于b的有几对 解析: 先把素数打表 运用算术基本定理 求出a的所有因数的个数 然后减去小于b的因数的个数 代码如下: #include <iostream> #i ...
- Goldbach`s Conjecture LightOJ - 1259 (素数打表 哥德巴赫猜想)
题意: 就是哥德巴赫猜想...任意一个偶数 都可以分解成两个(就是一对啦)质数的加和 输入一个偶数求有几对.. 解析: 首先! 素数打表..因为 质数 + 质数 = 偶数 所以 偶数 - 质数 = 质 ...
- hdoj--5104--Primes Problem(素数打表)
Primes Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 5104 素数打表水题
http://acm.hdu.edu.cn/showproblem.php?pid=5104 找元组数量,满足p1<=p2<=p3且p1+p2+p3=n且都是素数 不用素数打表都能过,数据 ...
随机推荐
- iOS FMDB中的使用
n使用事务 [queue inTransaction:^(FMDatabase *db, BOOL *rollback) { [db executeUpdate:@"INSERT INTO ...
- php 对象的一些特性
class person { private $name; private $age = 2; public function __construct($name,$age) { $this-> ...
- 文件读写IO
摘要:本文主要总结了以下有关文件读写的IO,系统调用与库函数. 1.初级IO函数:close,creat,lseek,open,write 文件描述符是一个整型数 1.1close 1.2int cr ...
- QOdbc 读写 excel
).toString(); ).toInt(); qDebug()<< name << age <<endl; } // 关闭数据库 db.close(); } i ...
- 关于mybatis插入数据库返回主键id
关于Sequence主键的数据库来说,如: <insert id="add" parameterType="vo.Category"> <se ...
- attr()和prop()的区别
引用以为一位大神的文章: http://www.365mini.com/page/jquery-prop.htm http://www.365mini.com/page/jquery-attr-vs- ...
- MongoDB学习笔记01
MongoDB的设计理念:能从服务器端转移到驱动程序来做的事,就尽量转移. 文档是MongoDB的核心概念.多个键及其关联的值有序的放置在一起便是文档.每种编程语言表示文档的方法不太一样. 文档的键是 ...
- java Object 类
Object o=new Object(); 其中有两个受保护的方法: 1.protected void finalize() 2.protected ...
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
首先做出整个应用的全局请求队列 package com.qg.lizhanqi.myvolleydemo; import android.app.Application; import com.and ...
- vs2010中socket链接错误LINK2019
解决方法参考下面链接: http://blog.163.com/strive_only/blog/static/893801682009225112354746/