CQUOJ 9711 Primes on Interval

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 aa + 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 xx + 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 ≤ 106a ≤ 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
/*
2016年4月21日23:49:25
题意:给出三个正整数a、b、k,求最小的L(1 ≤ L≤ b - a + 1)满足对于[a, b-L+1]中的任意一个数X,在[X, X+L-1]这L个数中,至少有k个素数。   如果不存在满足条件的L,输出-1.
解题思路:首先判断是否有解,即判断当L=b-a+1时是否有解,因此时L最大,若此时都无解,则肯定无解。
       若有解,则1 ≤ L≤ b - a + 1,二分求最小的L即可。

*/
 1 # include <iostream>
 2 # include <cstdio>
 3 # include <cstring>
 4 # include <algorithm>
 5 # include <queue>
 6 # include <vector>
 7 # include <cmath>
 8 # define INF 0x3f3f3f3f
 9 using namespace std;
10 const int N = 1e6 + 5;
11 int vis[N], cnt[N];
12
13 //  标记素数
14 void Sign()
15 {
16     int i, j;
17     int m = (int)sqrt(N);  // 可能是标记时 这个数不需要太大 后面的数可能在前面判断时都被标记过了
18     memset(vis, 0, sizeof(vis));  // 如果不开根号  好像会有问题...
19     for (i = 2; i <= m; i++){
20         if (vis[i]) continue;
21         for (j = i; j*i <= N; j++){
22             if (!vis[j*i])
23                 vis[j*i] = 1;
24         }
25     }
26 }
27 //  cnt[i] 表示 从1到 i一共有多少个素数
28 void Count()
29 {
30     Sign();
31     cnt[0] = cnt[1] = 0;
32     for (int i = 2; i <= N; i++){
33         if (!vis[i]) cnt[i] = cnt[i-1] + 1;
34         else cnt[i] = cnt[i-1];
35     }
36 }
37 // 判断当前的l是否满足题目要求
38 int Check(int a, int b, int k, int l)
39 {
40     int r = b - l + 1;
41     for (int i = a; i <= r; i++){
42         if (cnt[i + l - 1] - cnt[i - 1] >= k)
43             continue;
44         return 0;
45     }
46     return 1;
47 }
48 //  二分求 l的最小值
49 int Solve(int a, int b, int k)
50 {
51     int L = 1, R = b-a+1;
52     while (L <= R){
53         int mid = (L + R) / 2;
54         if (Check(a, b, k, mid))
55             R = mid - 1;
56         else L = mid + 1;
57     }
58     return L;
59 }
60
61 int main(void)
62 {
63     int a, b, k, ans;
64     Count();
65     while (~scanf("%d %d %d", &a, &b, &k)){
66         if (!Check(a, b, k, b - a + 1)) //此处判断 l的最大值是否符合
67             printf("-1\n");
68         else {
69             ans = Solve(a, b, k);
70             printf("%d\n", ans);
71         }
72     }
73
74     return 0;
75 }
时间: 2024-10-13 12:18:36

CQUOJ 9711 Primes on Interval的相关文章

Primes on Interval

AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1001000; #define  inf (1<<29) //上面的位运算还真心没有看懂 // p[i] is i-th prime's position bool pp[maxn]; //里面保存的是一

Primes on Interval(二分 + 素数打表)

Primes on Interval Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 237C Appoint description:  System Crawler  (2016-04-26) Description You've decided to carry out a survey in the theory

CodeForces 237C Primes on Interval

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).

CodeForces - 237C Primes on Interval(二分+尺取)

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

2016 ACM/ICPC Asia Regional Shenyang Online &amp;&amp; hdoj5901 Count primes Lehmer

Count primes Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Easy question! Calculate how many primes between [1...n]! Input Each line contain one integer n(1 <= n <= 1e11).Process to end of f

[LeetCode]Count Primes

题目:Count Primes 统计1-n的素数的个数. 思路1: 通常的思想就是遍历(0,n)范围内的所有数,对每个数i再遍历(0,sqrt(i)),每个除一遍来判断是否为素数,这样时间复杂度为O(n*sqrt(n)). 具体实现不在贴代码,过程很简单,两重循环就可以解决.但是效率很差,n较大时甚至会花几分钟才能跑完. 思路2: 用埃拉特斯特尼筛法的方法来求素数,时间复杂度可以达到O(nloglogn). 首先开一个大小为n的数组prime[],从2开始循环,找到一个质数后开始筛选出所有非素数

JsSIP.UA.JsSIP 总是返回错误:422 Session Interval Too Small

在JsSIP 中 JsSIP.UA.call 总是 返回错误:422 Session Interval Too Small 关于错详情在这篇文章中解释的比较详尽:http://www.cnblogs.com/yoyotl/p/4980817.html 但是没有JsSIP的解决方法 具体的解决方法如下: JsSIP.js中的搜索 SESSION_EXPIRES: 把这个参数改大,  大于120就行了, (要是还不行,就继续加大吧^.^) SESSION_EXPIRES: 90 加大: SESSIO

projecteuler Summation of primes

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. 译文: 10以下的素数之和为17,求出2000000以下的素数之和. ======================= 第一次code: 1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(

LeetCode Insert Interval

原题链接在这里:https://leetcode.com/problems/insert-interval/ AC Java: 1 /** 2 * Definition for an interval. 3 * public class Interval { 4 * int start; 5 * int end; 6 * Interval() { start = 0; end = 0; } 7 * Interval(int s, int e) { start = s; end = e; } 8