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

为什么会TLE,在Uva上都AC,为什么在这里就会超时?!(没用什么奇怪的方法啊

Posted by sightseer at 2015-06-18 16:07:55 on Problem 2689
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
#define range 50000 
#define range2 1000010
#define INF 0xFFFFFFF
typedef long long LL;
int prime[range],is_prime[range];
int a[range2],range_prime[range2];
int num_p;
void produce_prime()
{
	memset(is_prime,0,sizeof(is_prime));
	num_p=0;
	for(int i=2;i<range;i++)
	{
		if(is_prime[i]==0)
		{
			int j=i+i;
			prime[num_p++]=i;
			while(j<range)
			{
				is_prime[j]=1;
				j+=i;
			}
		}
	}
}
int main()
{
//	freopen("in.txt","r",stdin);
	LL left,r;
	produce_prime();
	while(scanf("%d%d",&left,&r)==2)
	{
		memset(a,0,sizeof(a));
		if(left==1)
		{
			a[0]=1;
		}
		for(int i=0;i<num_p;i++)
		{
			int k=(int)left/prime[i];
			LL m=k*prime[i];
			while(m<left || k<=1) 
				{
				 m+=(LL)prime[i];
				 k++;
				}
			for(LL j=m;j<=r;j+=prime[i])
			{
				a[j-left]=1;
			}
		}
		int minn=INF,maxx=-1;
		int minflag=-1,minf=-1,minlast=-1,maxf=-1,maxlast=-1,pre=-1;
		int co=0;
		for(LL i=left;i<=r;i++)
		{
			if(a[i-left]==0)
			{
				if(pre==-1)
					{
					 pre=i;
					}
					else
					{
						if(i-pre<minn)
						{
							minf=pre; minlast=i;
							minn=i-pre;
						}
						if(i-pre>maxx)
						{
							maxf=pre; maxlast=i;
							maxx=i-pre;
						}
						pre=i;
					}
				//last=i;
			}
		}
		if(minf==-1)
			printf("There are no adjacent primes.\n");
			else
				printf("%d,%d are closest, %d,%d are most distant.\n",minf,minlast,maxf,maxlast);
	}
	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