Codeforces 237C (Primes on Interval)二分
2016-10-17 19:36
417 查看
Primes on Interval
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
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】区间里面取最小长度 l 的区间里面最少有 k 个素数
二分区间找最小的就好了
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit
Status
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】区间里面取最小长度 l 的区间里面最少有 k 个素数
二分区间找最小的就好了
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define M 1000010 #define INF 0x3f3f3f3f bool prime[M]; int dp[M], a, b, k; void isprime() { prime[1] = 1; for(int i=2; i*i<M; i++) { if(!prime[i]) { for(int j=i*i; j<M; j+=i) { prime[j] = 1; } } } } void init() { dp[1] = 0; dp[0] = 0; for(int i=1; i<M; i++) { if(!prime[i]) { dp[i] = dp[i-1] + 1; } else { dp[i] = dp[i-1]; } } } bool judge(int mid) { for(int i=a; i<=b; i++) { if(i + mid - 1 > b) break; if(dp[i+mid-1] - dp[i-1] < k) { return false; } } return true; } int main() { isprime(); init(); while(scanf("%d%d%d", &a, &b, &k) != EOF) { int ans = -1, left = k, right = b - a + 1; int mid = 0; while(left <= right) { mid = (right + left) >> 1; if(judge(mid)) { ans = mid; right = mid - 1; } else { left = mid + 1; } } printf("%d\n", ans); } return 0; }
相关文章推荐
- 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
- CodeForces 237C Primes on Interval
- codeforces 237C.Primes on Interval
- CodeForce 237C Primes on Interval(二分+ 素数筛法)
- CodeForces 237C Primes on Interval
- 【Codeforcdes 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 (素数打表&二分)
- cf273C. Primes on Interval【二分】
- Primes on Interval 【打表+二分】
- Primes on Interval(素数打表+二分)
- codeforces 237c-Primes on Interva