codeforces 237C.Primes on Interval
2014-07-27 16:14
483 查看
C. Primes on Interval
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
这题做得我好蛋疼。本来用的方法是用一数组存1至t的素数个数至一个数组中p[t].然后求使p[x+l-1]-p[x]>=k的最大t。本来这个方法是对的,只要检验每个x是否满足即可。但我那时想,x可能要跑10^6,若再让l跑10^6不就会超时吗Orz..一我没注意看有2s,二我把二分用在了别的地方,不是判断l的大小上。所以我立即换了个方法。
因为我们要找k个素数,所以我就分别讨论原数组的素数总和小于k个,等于k个,大于k个的情形。可惜,我错在了忘记判断该数是否会超界。。
现在终于AC了。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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.
这题做得我好蛋疼。本来用的方法是用一数组存1至t的素数个数至一个数组中p[t].然后求使p[x+l-1]-p[x]>=k的最大t。本来这个方法是对的,只要检验每个x是否满足即可。但我那时想,x可能要跑10^6,若再让l跑10^6不就会超时吗Orz..一我没注意看有2s,二我把二分用在了别的地方,不是判断l的大小上。所以我立即换了个方法。
因为我们要找k个素数,所以我就分别讨论原数组的素数总和小于k个,等于k个,大于k个的情形。可惜,我错在了忘记判断该数是否会超界。。
现在终于AC了。
#include<stdio.h> #include<string.h> int p[1000001],cun[1000009],tot; void biao(int n) { memset(p,0,sizeof(p)); p[1]=1;p[0]=1;tot=0; for(int i=2;i<n;i++) { if(!p[i]) { cun[tot++]=i; for(int j=i+i;j<n;j+=i) p[j]=1; } } } int main() { int T,i,k,j,a,b,xiao,da; biao(1000001); int front=0,rear=tot-1,mid; while(~scanf("%d%d%d",&a,&b,&k)) { { front=0; rear=tot-1; int count,ans; while(front<rear) { mid=front+(rear-front)/2; if(cun[mid]>=a)rear=mid; else front=mid+1; } front=rear; count=0; while(cun[rear]<=b) { count++; if(count<=k){rear++;if(rear==tot)break;} else break;; } if(count==k+1) { ans=cun[rear-1]-a+1; for(;;) { a=cun[front]; if(ans<cun[rear]-a)ans=cun[rear]-a; rear++;front++; if(cun[rear]-1>=b||rear==tot)break; //我就是忘记判断rear==tot? } if(ans<b-cun[front]+1)ans=b-cun[front]+1; printf("%d\n",ans); } else if(count==k) if(cun[rear-1]-a+1>b-cun[front]+1) printf("%d\n",cun[rear-1]-a+1); else printf("%d\n",b-cun[front]+1); else printf("-1\n"); } } 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(素数统计)
- CodeForces-237C- Primes on Interval
- 【CodeForces】237C - Primes on Interval(二分)
- codeforces 237C. Primes on Interval(二分)
- CodeForce 237C Primes on Interval(二分+ 素数筛法)
- codeforces 237 C. Primes on Interval
- codeforces 237c-Primes on Interva
- codeforces 237-C. Primes on Interval(尺取)
- 237C - Primes on Interval
- 【Codeforcdes 237C. Primes on Interval】+ 二分
- Primes on Interval(素数打表+二分)
- Primes on Interval+codeforces+筛选法打表
- CodeForces - 237C Primes on Interval 【二分】