Primes on Interval(CF-237C)
Problem 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.
Examples
Input
2 4 2
Output
3
Input
6 13 1
Output
4Input
1 4 3
Output
-1
题意:给出 a、b、k 三个数,要求在区间 [a,b] 中找一个长度 l,使得对区间中任意一个位置到这个位置加上 l 后的子区间中存在 k 个质数,求最小的长度 l
思路:
首先用素数筛打一个素数表
由于 a、b 的数据范围可达 1E6,单纯的暴力一定会 TLE,而题目的关键是要求一个最小的长度,那么可以使用二分去枚举长度,找一个最小的解
需要注意的是,如果单纯使用素数表进行统计的话,依然会 TLE,需要考虑对素数表做出优化,求一个素数表的前缀和,用于计算第 i 个数前有多少个素数
Source Program
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;int prime[N],cnt;
bool bprime[N];
int sum[N];
void make_prime() {memset(bprime,false,sizeof(bprime));bprime[0]=true;bprime[1]=true;for(int i=2;i<N;i++){if(!bprime[i]){prime[cnt++]=i;for(int j=i*2;j<N;j+=i){bprime[j]=true;}}}
}bool judge(int a,int b,int k,int x) {for(int i=a;i<=b-x+1;i++)if(sum[i+x-1]-sum[i-1]<k)return false;return true;
}
int main() {make_prime();for(int i=1;i<=N;i++) {sum[i]=sum[i-1];if(!bprime[i])sum[i]++;}int a,b,k;scanf("%d%d%d",&a,&b,&k);int res=INF;int left=1,right=b-a+1;while(left<right){int mid=(left+right)/2;if(judge(a,b,k,mid))right=mid;elseleft=mid+1;}if(judge(a,b,k,left))printf("%d\n",left);elseprintf("-1\n");return 0;
}
Primes on Interval(CF-237C)相关推荐
- 【解题报告】随便练练二(CF 2300)
[解题报告]随便练练二(CF 2300) A:Antimatter | CF383D 题意 思路 :DP 代码 B:Physical Education Lessons | CF915E 题意 思路一 ...
- CodeForce 237C Primes on Interval(二分+ 素数筛法)
题目链接:http://codeforces.com/problemset/problem/237/C Primes on Interval time limit per test 1 second ...
- Commentator problem(CF 2)
题目链接 题目大意: 给定三个圆,询问是否存在点满足该点与三个圆夹角均相等,若存在多组解返回夹角最大值. 圆外一点到两圆夹角均相等: 即 sina = sinb = r1 / d1 = r2 / d2 ...
- D. Make a Power of Two(cf#739DIV3)
D. Make a Power of Two 链接: link. 题意: 找出将数字转换为 2 的任意幂的最小移动次数. 题解: 先将2的xxx次幂的结果以字符串形式保存,输入字符串nnn后,因为存在 ...
- Web of Lies(CF 1548A)
这是今天在打个人赛时碰见的一道题,是一道半图论半思维的题. Web of Lies 题目大意不难理解,在这里只需要注意一些细节.在加边时,只有当cnt[min]的值为1时答案才应该减1,而不是当cnt ...
- Magic Powder - 2 (CF 670_D)
http://codeforces.com/problemset/problem/670/D2 The term of this problem is the same as the previous ...
- 【解题报告】博弈专场 (CF 2000~2200)前五题
[解题报告]博弈专场 (CF 2000+)前五题 A:Fox and Card Game | CF388C 题意 思路 代码 B:Berzerk | CF786A 题意 思路 代码 C:Ithea P ...
- 软件设计师提纲+复习资料整理(上午题)
文章目录 软件设计师考试大纲 上午题(选择题) 一.计算机组成原理 考点:CPU结构组成 考点:原码.反码.补码定点整数范围 考点:浮点数表示 考点:RISC和CISC计算机的区别 考点:奇校验与偶校 ...
- Leet 题目整理归类 - 快速通道 (持续更新)
刷Leet 5个月了,先总结一下,这里算是每题的快速通道.自己做个记录便于以后重温算法.如果能帮到别人就更好了. 本人是算法新手,如果对于一些题目读者有更好的实现方法,如能不吝赐教,万分感谢. [DF ...
最新文章
- Java学习之for语句
- ubuntu 如何在任意终端不填加./就可以执行文件类似ls cd cp
- 谁今天收到鸿蒙系统推送,鸿蒙系统正式推送,只有部分高端机才能收到
- 一线大厂青睐的前端人,90%满足这3个条件
- 火狐浏览器表单不跳转_坑爹火狐浏览器会记录表单数据,导致服务器控件点击事件出bug...
- pytorch 中 torch.cat 函数的使用
- 没了珊瑚虫你用谁?八大QQ主流修改版大比拼
- Oracle体系结构图(思维导图及详解)
- QCC3040---芯片数据手册 ADK release data sheet
- Cousera- software security
- 查看 PCD 点云 windows
- 设置ubuntu终端光标开启自动显示
- handsome对应php文件,基于handsome主题的一些美化总结
- 抖音云控系统多少钱一套?
- A Unified Multi-scale Deep Convolutional_Neural Network for Fast Object Detection 论文笔记
- android 窗口切换花屏,分享Android4平台二级页面滚动花屏问题的解决方案v1.0.0
- 苹果cms8整合dplayer播放器
- db2 修改表空间自增长_db2自动调整表空间的大小 - 木子日京的个人空间 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
- 雷达视频化安防软件——RadarVison
- CVE-2020-1938 Tomcat AJP漏洞复现