Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

暴力也32ms啊,献上挫代码

Posted by TSERROF at 2012-10-08 20:54:15 on Problem 3518
#include<iostream>
#include <cstring>
using namespace std;
const int N = 25600000;
bool a[N];
int p[N];
int n;
void Prime() 
{
	memset(a, 0, n*sizeof(a[0]));
	int num = 0, i, j;
	for(i = 2; i < n; ++i) 
	{
		if(!(a[i])) p[num++] = i;
		for(j = 0; (j<num && i*p[j]<n); ++j) 
		{
			a[i*p[j]] = 1;
			if(!(i%p[j])) break;
		}
	}
	a[0]=true,a[1]=true;
}
int main()
{
	n=1299710;
	Prime();
	int number;
	while(cin>>number && number)
	{
		int i,j;
		for(i=number;i>=2;--i)
		{
			if(a[i])continue;
			break;
		}
		for(j=number;j<=1299709;++j)
		{
			if(a[j])continue;
			break;
		}
		cout<<j-i<<endl;
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator