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

超快的算法,不过不知道为什么RE,所有的数据都过了出来出现负数(附code)

Posted by RUNSLOWLY at 2009-03-15 20:46:10 on Problem 3511
#include<stdio.h>
#include<iostream>
using namespace std;

void sum(__int64 n,bool prime[])
{
	__int64 k=2;
	memset(prime,0,(n+10)*sizeof(bool));
	while(k<=n)
	{
	   for(__int64 i=2*k;i<=n;i+=k)
		   prime[i]=1;
	   while(1)
	   {
		   k++;
		   if(prime[k]==0)
		   {			
			   break;
		   }
	   }
	}
}

int main()
{
	bool prime[1000016]={1,1,0};
	__int64 a,b;
	__int64 num1,num2;
	prime[0]=prime[1]=1;
	prime[2]=0;
	while(1)
	{
		scanf("%I64d%I64d",&a,&b);
		if(a==b && b==-1)
			break;
		num1=num2=0;
		sum(b,prime);
		for(__int64 i=a;i<=b;i++)
		{
			if(prime[i]==0)
			{
				num1++;
				if((i-1)%4==0)
					num2++;
			}
		}
	printf("%I64d %I64d %I64d %I64d\n",a,b,num1,num2);
	}
	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