【codeforce237C】Primes on Interval

本文探讨了如何找到一个最小长度L,使得在给定区间[a,b]内的任意子区间[x,x+L-1]中都至少包含k个素数。通过预处理素数表并采用二分查找的方法来高效解决问题。

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

Example
Input
2 4 2
Output
3
Input
6 13 1
Output
4
Input
1 4 3
Output

-1

题意:给你一个闭区间[a,b],求一个最小的L,使得在区间[a,b-L+1]内任取一个数x,可以满足在x,x+1,x+2,……,x+L-2,x+L-1内至少包含k个素数。(1<=a,b,k<=10^6)

题解:素数打表,统计出每个区间有多少个素数,对L二分查找

code:

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a,b,k;
int c[1000000+11];
int f[1000011];
int cheak(int mid){
for(int i=a;i<=b-mid+1;i++)
{
	if(f[i+mid-1]-f[i-1]<k)
return 0;
}	
return 1;
}
int main()
{
	for(int i=2;i<=1000011;i++)
	{
		f[i]=f[i-1];
		if(c[i]==1) continue;
		f[i]++;
		for(int j=2*i;j<=1000011;j+=i){
			c[j]=1;
		}
	}//素数打表
	while(~scanf("%d%d%d",&a,&b,&k)){
		if(f[b]-f[a-1]<k) {
			printf("-1\n");return 0;
		}
			int l=1;int r=b-a+1,ans;
			while(l<=r){
				int mid=(l+r)>>1;
				if(cheak(mid)) ans=mid,r=mid-1;
				else l=mid+1;
			}
			printf("%d\n",ans);
	       
		}
	
	return 0;
}


### 解题思路 在Codeforces 617C浇花问题里,目标是确定在给定天数、花的初始高度、每次可浇水的连续花的数量等条件下,能让最小高度的花达到的最大高度。这是一个优化问题,核心在于找到一种浇水策略,使得最终最小高度的花尽可能高。 一般可采用二分查找的方法来解决。先确定最小高度花的可能取值范围,也就是从当前最小高度到一个理论上可能达到的最大高度。然后在这个范围内进行二分查找,对于每一个中间值,检查是否能够通过合理的浇水操作让所有花的高度都不低于这个中间值。 检查的过程是模拟浇水操作,从左到右遍历每一朵花,如果某朵花的高度低于当前检查的中间值,就从这朵花开始往后选择连续的 `w` 朵花进行浇水,直到这朵花达到中间值。同时要记录浇水的次数,若浇水次数超过了给定的天数 `m`,则说明这个中间值无法达到,需要缩小右边界;反之,则可以尝试更大的值,扩大左边界。 ### 代码实现 ```python # 检查是否能让所有花的高度都不低于 h def can_reach_height(heights, m, w, h): n = len(heights) watered = [0] * n total_watering = 0 current_watering = 0 for i in range(n): # 计算当前位置的实际浇水情况 if i - w >= 0: current_watering -= watered[i - w] need = max(0, h - (heights[i] + current_watering)) watered[i] = need current_watering += need total_watering += need if total_watering > m: return False return True # 二分查找最大的最小高度 def find_max_min_height(heights, m, w): left = min(heights) right = min(heights) + m while left < right: mid = (left + right + 1) // 2 if can_reach_height(heights, m, w, mid): left = mid else: right = mid - 1 return left # 读取输入 n, m, w = map(int, input().split()) heights = list(map(int, input().split())) # 计算并输出结果 result = find_max_min_height(heights, m, w) print(result) ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值