ZCMU-1022-Primes on Interval
来源:互联网 发布:动态称重算法 编辑:程序博客网 时间:2024/06/05 06:31
1022: Primes on Interval
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 145 Solved: 65
[Submit][Status][Web Board]
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
Everay 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
Sample Output
#include <stdio.h>#include <math.h>#include <string.h>using namespace std;#define MAX 1000100int sum[MAX]={0};int su[MAX]={0};int a,b,k;void panduan(){ int i,j; for(i=2;i<MAX;i++) { sum[i]=sum[i-1];//表示前一个区间内有的素数给现在这个区间 if(su[i]==0) { sum[i]++;//如果i是素数则这个区间内的素数加1 for(j=1;i*j<=MAX;j++)//筛法求素数 su[i*j]=1; } }}int check(int L){ int i; for(i=a;i<=b-L+1;i++) { if(sum[i+L-1]-sum[i-1]<k) return 0; } return 1;}int main(){ panduan(); int i,j,left,right,mid,L; while(~scanf("%d%d%d",&a,&b,&k)) { if(sum[b]-sum[a-1]<k)//判断a到b的范围内的素数如果小于k那接下来是不可能有它的子区间的素数是大于k的 { printf("-1\n");//这里为什么要用a-1,我们把样例1带入,区间是2到4,如果我们算的是sum[b]-sum[a]那肯定不对 continue;//如果a是素数呢,那这样相当于你多减去一个素数了 } left=1; right=b-a+1; while(left<=right)//二分法查找 { mid=(left+right)/2; if(check(mid)) { L=mid; right=mid-1;//继续寻找小的 } else { left=mid+1;//往后找到L的几率比较大,如果中间位置不行了的话 } } printf("%d\n",L); } return 0;}
- ZCMU-1022-Primes on Interval
- Primes on Interval
- codeforces237C Primes on Interval
- 【codeforces237C】Primes on Interval
- 【Primes on Interval】
- Primes on Interval
- 【codeforce237C】Primes on Interval
- 237C - Primes on Interval
- cf273C. Primes on Interval【二分】
- codeforces 237C.Primes on Interval
- Primes on Interval+codeforces+筛选法打表
- CodeForces-237C- Primes on Interval
- Codeforces 237C Primes on Interval 【二分】
- 【 Codeforces 237C Primes on Interval】+ 二分
- CodeForces 237C Primes on Interval
- CodeForces - 237C Primes on Interval 【二分】
- Codeforces 237C Primes on Interval
- Primes on Interval 【打表+二分】
- Android dialog 去除虚拟按键
- android复习路之二叉树层次遍历并且分层
- Andrew算法求凸包
- JS跳转手机QQ的聊天页面
- freeswitch函数之switch_separate_string字符串分割
- ZCMU-1022-Primes on Interval
- 滴滴出行2017秋招笔试编程题(一)——连续最大和、末尾0的个数、进制转换
- jQuery Mobile
- java8函数式编程之二:函数式接口的引用
- 以太网PHY 芯片之 MII/MDIO接口详解
- 数据结构基本结构 数据结构期末复习
- qt 错误记录
- MySQL 的 TRUNCATE 和 DELETE 区别
- 微信小程序开发教程--从零开始