『NYIST』第八届河南省ACM竞赛训练赛[正式赛一] E题
2016-04-07 20:46
459 查看
E - ECrawling in process...Crawling failedTime Limit:1000MSMemory Limit:262144KB 64bit IO Format:%I64d & %I64uSubmitStatusPracticeCodeForces 237CDescriptionYou'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 integerl(1 ≤ l ≤ b - a + 1) such that for any integerx(a ≤ x ≤ b - l + 1) amongl integers x,x + 1, ...,x + l - 1 there are at least k prime numbers.Find and print the required minimum l. If no valuel meets the described limitations, print -1.InputA single line contains three space-separated integers a, b, k (1 ≤ a, b, k ≤ 106; a ≤ b).OutputIn a single line print a single integer — the required minimuml. If there's no solution, print -1.Sample InputInput
2 4 2Output
3Input
6 13 1Output
4Input
1 4 3Output
-1
#include<algorithm>#include<stdio.h>#include<string.h>using namespace std;#define maxn 1000005int p[maxn]= {0}; //前i位有sum[i]个素数int d[maxn];//是素数标记为1,否则标记为0int a,b,k;void shaifa(){int i,j;d[0]=d[1]=1;for(i=2; i<maxn; i++){p[i]=p[i-1];if(!d[i]){p[i]++;for(j=2*i; j<maxn; j+=i)//或者:for(j=1;i*j<maxn;j++){d[i*j]=1;}{d[j]=1;}}}}bool check(int x)//传入二分查找的中间值{for(int i=a; i<=b-x+1; i++)if(p[i+x-1]-p[i-1]<k)//此区间的素数小于kreturn 0;return 1;//满足条件返回1}int main(){shaifa();while(~scanf("%d%d%d",&a,&b,&k)){if(p[b]-p[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;//缩小范围继续查找}elseleft=mid+1;//改变左边界}printf("%d\n",num);}}[/code]
相关文章推荐
- nyoj 91 阶乘之和
- 李娜跟姜山
- 返回一个整数数组中最大子数组的和。
- Js 对 浏览器 的 URL的操作
- OpenCV 无法启动此程序,因为计算机中丢失opencv_core249.dll。请尝试重新安装改程序已解决此问题
- 剑指offer-面试题22.栈的压入,弹出序列
- 客户机程序与服务器程序
- dispatcherservlet详解
- Mybatis 一对多 查询
- Swift UITextField/UITextView(placeholder的制作)
- HDOJ 1279 验证角谷猜想
- 04-07博客
- HDOJ 1279 验证角谷猜想
- 自定义tabBar在push到下一个界面的时候实现隐藏
- C语言问卷调查表
- BZOJ2741: 【FOTILE模拟赛】L
- 12、Mysql优化常见问题
- 剑指offer-面试题21.包含min函数的栈
- 升级Xcode之后VVDocumenter-Xcode不能用的解决办法
- 数据库增删改查(二)