Discription 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.
Examples Input 2 4 2 Output 3 Input 6 13 1 Output 4 Input 1 4 3 Output -1
题意 要求最小的l,是长度为l的子串都可以包含至少k个素数。
思路 从a开始记录前缀和,暴力判断,具体看代码。
AC代码
#include<bits/stdc++.h> using namespace std; int vis[1000005],sum[1000005]; int a,b,k; void prime(int x) { for(int i=2;i<=b;i++) { for(long long j=(long long)i*(long long)i;j<=b;j+=i) vis[j]=0; } } int main() { cin>>a>>b>>k; for(int i=2;i<=b;i++) vis[i]=1; prime(b); sum[a-1]=0; for(int i=a;i<=b;i++) { sum[i]=sum[i-1]+vis[i]; } if(sum[b]<k) { cout<<-1<<endl; return 0; } int l=1; for(int i=a-1;i<=b-l;i++) { //cout<<sum[i+l]-sum[i]<<"*"<<endl; while(l>=1&&l<=b-a+1&&i+l<=b&&sum[i+l]-sum[i]<k)//注意条件 { l++; //cout<<sum[i+l]-sum[i]<<"&"<<endl; //cout<<l<<endl; } } cout<<l<<endl; return 0; }