Problem E 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 aa + 1, ..., b (a ≤ b). You want to find the minimum integer l (1 ≤ l ≤ b - a + 1) such that for any integerx (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

一边筛素数,一边处理出一个前缀和sum sum(i)表示[1,i]中有多少素数 那么我们每次查询区间[l,r]中有多少素数,直接查sum[r]-sum[l-1]就可以了 接下去我们按照题意,对答案L进行二分就可以了

 1 #include <iostream>
 2 using namespace std;
 3
 4 const int maxn = 1000001;
 5 int sum[maxn],a,b,k;
 6 bool pri[maxn];
 7 void init(){
 8     for(int i = 2;i < maxn;i++){
 9         sum[i] = sum[i-1];
10         if(pri[i])  continue;
11         sum[i]++;
12         for(int j = i+i;j < maxn;j += i)
13             pri[j] = 1;
14     }
15 }
16
17 bool check(int mid){
18     for(int i = a;i <= b-mid+1;i++){
19         if(sum[i+mid-1]-sum[i-1] < k) return 0;
20     }
21     return 1;
22 }
23
24 int main(){
25     init();
26     cin.sync_with_stdio(false);
27     cin>>a>>b>>k;
28     if(sum[b]-sum[a-1] < k){
29         cout<<"-1"<<endl;
30         return 0;
31     }
32     int l = 1,r = b-a+1,ans;
33     while(l <= r){
34         int mid = (l+r)>>1;
35         if(check(mid))  ans = mid,r = mid-1;
36         else    l = mid+1;
37     }
38     cout<<ans<<endl;
39 }

Problem E CodeForces 237C

时间: 2024-10-11 17:19:29

Problem E CodeForces 237C的相关文章

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 ≤

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

Problem F CodeForces 16E

Description n fish, numbered from 1 to n, live in a lake. Every day right one pair of fish meet, and the probability of each other pair meeting is the same. If two fish with indexes i and j meet, the first will eat up the second with the probability 

Problem B Codeforces 295B 最短路(floyd)

Description Greg has a weighed directed graph, consisting of n vertices. In this graph any pair of distinct vertices has an edge between them in both directions. Greg loves playing with the graph and now he has invented a new game: The game consists

Problem - D - Codeforces Fix a Tree

Problem - D - Codeforces  Fix a Tree 看完第一名的代码,顿然醒悟... 我可以把所有单独的点全部当成线,那么只有线和环. 如果全是线的话,直接线的条数-1,便是操作数. 如果有环和线,环被打开的同时,接入到线上.那就是线和环的总数-1. 如果只有环的话,把所有的环打开,互相接入,共需n次操作. #include <cstdio> #include <algorithm> using namespace std; const int maxn =

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem F (Codeforces 831F) - 数论 - 暴力

Vladimir wants to modernize partitions in his office. To make the office more comfortable he decided to remove a partition and plant several bamboos in a row. He thinks it would be nice if there are n bamboos in a row, and the i-th from the left is a

Educational Codeforces Round 21 Problem F (Codeforces 808F) - 最小割 - 二分答案

Digital collectible card games have become very popular recently. So Vova decided to try one of these. Vova has n cards in his collection. Each of these cards is characterised by its power pi, magic number ci and level li. Vova wants to build a deck

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo