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且都是素数 不用素数打表都能过,数据 ...
随机推荐
- Spring Tool Suit 在Eclipse上的安装
登录http://spring.io/tools/sts/all 下载所需的Spring Tool Suit安装包 我用的是springsource-tool-suite-3.6.1.RELEASE- ...
- git commit -am 合并操作
二,合并的操作 1, 首先按需修改文件 echo >> lz66303.txt 2, 然后按需提交被修改的文件到HEAD缓存区,并把这个修改记录到分支中 git commit -am&qu ...
- JavaScript ----------------- 寄生式继承
寄生式继承 寄生式继承是于原型式继承紧密相关的一种思路.寄生式基础的思路与寄生构造函数和工厂模式类似,既创建一个仅用于封装继承过程的函数,该函数内部以某种方式来增强对象,最后再像真地是它做了所有工作一 ...
- mysql Fatal error encountered during command execution
由于在操作中使用了自定义参数. 所以得在连接字符串中加上Allow User Variables=True: 表示允许用户自定义参数.
- Unity脚本——Csharp
打印输出: Debug.Log(""); 游戏流程函数: Awake():在对象被创建的时候调用. Start():在Awake()方法之后执行.在脚本禁用后不会执行. updat ...
- uva 508 Morse Mismatches
Samuel F. B. Morse is best known for the coding scheme that carries his name. Morse code is still us ...
- 哪几个数的阶乘末尾有n个零?
题目:哪几个数的阶乘末尾有n个0?其中n是一个正整数,从键盘输入. int main( void ) /* name: zerotail.cpp */ { int num, n, c, m; cout ...
- C++中类的内存空间大小(sizeof)分析
注意类大小与结构体大小还是有一些不同的,类里面还包含成员函数(特别是虚函数),结构体中一般只有数据成员. 首先明确各数据类型占多大的空间.例如int到底是占2字节还是4字节空间: 在TC里,int是2 ...
- git删除分支
git branch -d branchname删除一个分支需要具备的条件: 1 如果待删除的分支没有upstream branch,那么待删除的分支需要合并到HEAD上,否则需要使用-D强制删除 2 ...
- Servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...