Codeforces 237c:primes on Interval (prime number + two points)

Source: Internet
Author: User
Tags int size time limit
C. Primes on Interval time limit per test 1 second memory limit per test, megabytes input standard input output Standar D Output

You ' ve decided to carry out a survey in the theory of prime numbers. Let us remind you the A prime number is a positive an integer that has exactly a 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 this for any integer x (a≤x≤b-l + 1) among L integer s x, x + 1, ..., x + l-1 There is 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


Title: Give you the number A, B, K, let you ask for a * min * of the number L, so that in a, B interval of any length of the sub-continuous interval of l at least K primes.

Problem-Solving ideas: The number of characters to play the table, + a two points.

The code is as follows:

#include <cstdio>
int flag[1000010];
int sum[1000010];
int a,b,k,mid;
void Dabiao ()//Prime table, Sum[i] indicates the number of primes (including i) {flag[0]=flag[1]=1) in front of I
	;
	for (int i=2;i<1000010;i++)
	{
		if (flag[i]==1)
		continue;
		for (int j=i*2;j<1000010;j=j+i)
		{
			flag[j]=1;
		}
	}
	sum[0]=0;
	for (int i=1;i<1000010;i++)
	{
		if (flag[i]==0)
		{
			sum[i]=sum[i-1]+1;
		}
		else
		{
			sum[i]=sum[i-1];
}}}
	the range for 
	(int i=a;i<=size;i++)
	{
		if (sum[i+o-1]-) in the title of the bool judge (int o) {int size=b-o+1;// SUM[I-1]<K)//i+o-1 is the number of digits from the point of I, i-1 explanation: The number of characters between A~b =sum[b]-sum[a-1],a-1 is equivalent to i-1 
		{
			return false;
		}
	}
	return true;
}
int main ()
{
	dabiao ();
	scanf ("%d%d%d", &a,&b,&k);
	int l=1,r=b-a+1;//Test instructions set the left and right interval 
	int ans=-1;//cannot be found when output-1, so initialized to 1 while 
	(l<=r)//binary 
	{
		mid= (l+r) /2;
		if (judge (mid))
		{
			ans=mid;
			r=mid-1;
		}
		else
		{
			l=mid+1;
		}
	}
	printf ("%d\n", ans);
	return 0;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.