CodeForces-237C- Primes on Interval
2016-04-05 20:20
561 查看
E - E
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 237C
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
Input
2 4 2
Output
3
Input
6 13 1
Output
4
Input
1 4 3
Output
-1
题意:给出区间a,b,以及数量k,使得x到x+l-1这个区间包含最少k个素数,其中x,l满足不等式a ≤ x ≤ b - l + 1和1 ≤ l ≤ b - a + 1,输出最小的l;
思路:既然和素数有关,一定少不了筛法的思想,同时借用nyoj士兵杀敌一的思想,用数组存下前i位的素数数量,这样只需扫描一次存下来就可以O(1)访问了。接着就是二分查找,大约O(nlogn)的时间复杂度查找到最小的l。
代码
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
Practice
CodeForces 237C
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
Input
2 4 2
Output
3
Input
6 13 1
Output
4
Input
1 4 3
Output
-1
题意:给出区间a,b,以及数量k,使得x到x+l-1这个区间包含最少k个素数,其中x,l满足不等式a ≤ x ≤ b - l + 1和1 ≤ l ≤ b - a + 1,输出最小的l;
思路:既然和素数有关,一定少不了筛法的思想,同时借用nyoj士兵杀敌一的思想,用数组存下前i位的素数数量,这样只需扫描一次存下来就可以O(1)访问了。接着就是二分查找,大约O(nlogn)的时间复杂度查找到最小的l。
代码
#include<algorithm> #include<iostream> #include<stdio.h> #include<math.h> #include<string.h> #include<string> using namespace std; const int maxn=1000005; int sum[maxn];//前i位有sum[i]个素数 int is_sushu[maxn];//是素数标记为1,否则标记为0 int a,b,k;//含义如题 void shaifa() { memset(sum,0,sizeof(sum)); memset(is_sushu,0,sizeof(is_sushu));//不是素数就标记为1 for(int i=2; i<maxn; i++) { sum[i]=sum[i-1]; if(is_sushu[i]==0)//如果是素数 { sum[i]++;//素数数量加一 for(int j=1; i*j<=maxn; j++) //筛法 is_sushu[i*j]=1; } } } bool check(int x)//传入二分查找的中间值 { for(int i=a; i<=b-x+1; i++) if(sum[i+x-1]-sum[i-1]<k)//此区间的素数小于k return 0; return 1;//满足条件返回1 } int main() { shaifa(); while(~scanf("%d%d%d",&a,&b,&k)) { if(sum[b]-sum[a-1]<k)//取极限,如果不满足条件 { printf("-1\n"); return 0; } int left=1;//左边界 int right=b-a+1;//右边界 int num; while(left<=right)//二分查找 { int mid=(left+right)/2;//中间值 if(check(mid))//满足条件 { num=mid; right=mid-1;//缩小范围继续查找 } else left=mid+1;//改变左边界 } printf("%d\n",num); } }
相关文章推荐
- 第五周 项目1(3)
- NDK-JNI实战教程(四)再谈新工具及NDK开发调试
- tsung测试openfire时从CSV文件读取user信息
- tsung无密码验证
- iOS cookie值的读取和存储
- 栈的压入、弹出序列
- 高效率的素数判断方法
- Storm源码分析--Nimbus-data
- MapReduce程序运行中的排序问题
- 学习笔记(第六周)
- 第五周上机实践项目 项目4 - 长方柱类
- Tsung参数说明
- CSS导航菜单--三状态玻璃效果菜单(双层滑动门应用)
- ZMY_fragment传值
- AVAudioPlayer播放音乐
- PHP解决Xss跨域攻击以及sql注入等危险字符串方案类库
- Git学习笔记
- 如何向数据库插入带有单引号(')的字符串?
- HTTP常见状态码
- ReactNative 代码调试方法