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且都是素数 不用素数打表都能过,数据 ...
随机推荐
- qt model/view 架构基础介绍之QTableWidget
# -*- coding: utf-8 -*- # python:2.x #说明:QTreeWidget用于展示树型结构,也就是层次结构同前面说的 QListWidget 类似,这个类需要同另外一个辅 ...
- IOS 设备信息读取
let infoDictionary = NSBundle.mainBundle().infoDictionary let appDisplayName: AnyObject? = infoDicti ...
- Timer.3 - Binding arguments to a handler
In this tutorial we will modify the program from tutorial Timer.2 so that the timer fires once a sec ...
- "a newer version of unity web player is required but the auto-update failed"
问题背景描述: 项目采用winform调用unity web player作为播放器在客户端使用. 在有些环境会出现标题所示错误. 经过一翻研究后发现是插件在向服务器请求更新以下文件时报http 30 ...
- [转]CSS vertical-align属性详解 作者:黄映焜
CSS vertical-align属性详解 posted @ 2014-08-26 17:44 黄映焜 前言:关于vertical-align属性. 实践出真知. 垂直居中. 第二种用法. ...
- ArcGIS10.3.1于2015年6月发布
http://www.esrichina.com.cn/sectorapplication/ArcGIS%2010.3/index.html
- iOS开发~interface Builder(简称 IB) 界面构建器
1.interface Builder 设置界面 1.1 是什么? 一个可视化的界面编辑工具软件,在xcode4之后整合到了xcode中 1.2 作用? 通过可视化的界面设置,能够少写或不写代码而完成 ...
- Xcode6和Xcode5获取app名字
1.在Xcode5下,获取程序名字(app name)的方法为: NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionar ...
- ZendFramework 两种安装方式
1. 在线安装(基于composer) Zend 应用程序骨架 GitHub 地址: https://github.com/zendframework/ZendSkeletonApplication ...
- Pie(hdu 1969 二分查找)
Pie Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...