日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Primes on Interval(CF-237C)

發布時間:2025/3/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Primes on Interval(CF-237C) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

Examples

Input

2 4 2

Output

3

Input

6 13 1

Output
4

Input

1 4 3

Output
-1

題意:給出 a、b、k 三個數,要求在區間 [a,b] 中找一個長度 l,使得對區間中任意一個位置到這個位置加上 l 后的子區間中存在 k 個質數,求最小的長度 l

思路:

首先用素數篩打一個素數表

由于 a、b 的數據范圍可達 1E6,單純的暴力一定會 TLE,而題目的關鍵是要求一個最小的長度,那么可以使用二分去枚舉長度,找一個最小的解

需要注意的是,如果單純使用素數表進行統計的話,依然會 TLE,需要考慮對素數表做出優化,求一個素數表的前綴和,用于計算第 i 個數前有多少個素數

Source Program

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<bitset> #define EPS 1e-9 #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long const int MOD = 1E9+7; const int N = 1000000+5; const int dx[] = {-1,1,0,0,-1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std;int prime[N],cnt; bool bprime[N]; int sum[N]; void make_prime() {memset(bprime,false,sizeof(bprime));bprime[0]=true;bprime[1]=true;for(int i=2;i<N;i++){if(!bprime[i]){prime[cnt++]=i;for(int j=i*2;j<N;j+=i){bprime[j]=true;}}} }bool judge(int a,int b,int k,int x) {for(int i=a;i<=b-x+1;i++)if(sum[i+x-1]-sum[i-1]<k)return false;return true; } int main() {make_prime();for(int i=1;i<=N;i++) {sum[i]=sum[i-1];if(!bprime[i])sum[i]++;}int a,b,k;scanf("%d%d%d",&a,&b,&k);int res=INF;int left=1,right=b-a+1;while(left<right){int mid=(left+right)/2;if(judge(a,b,k,mid))right=mid;elseleft=mid+1;}if(judge(a,b,k,left))printf("%d\n",left);elseprintf("-1\n");return 0; }

?

總結

以上是生活随笔為你收集整理的Primes on Interval(CF-237C)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。