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

1000个数为一个桶,就快了

Posted by wcfairytale at 2011-10-09 23:00:07 on Problem 3978
#include<stdio.h>
#include<string.h>

bool n[100001];
int can[101];
void find_primes()
{
	memset(n,true,sizeof(n));

	int i,j;
	n[0] = false;
	n[1] = false;

	for(i = 2; i < 350; i++)
	{
		if(n[i])
		{
			for(j = i*i; j < 100000; j+=i)
			{
				n[j] = false;// enter here 19W times 
			}
		}
	}

	int start, end;
	memset(can,0,sizeof(can));

	for(i = 0; i < 100; i++)//0~999,1000~1999...
	{
		int start = i*1000;
		int end = start + 1000;
		for(j = start; j < end; j++)
		{
			if(n[j])can[i]++;
		}
	}
}

int main()
{
	find_primes();
	
	int i,j;
	int a,b;
	int re;
	//for(i=0;i<1000;i++)if(n[i])printf("%d ",i); printf("\n");
	while(scanf("%d%d",&a,&b))
	{
		if(-1 == a && -1 == b)break;

		re = 0;

		for(i=a; i<=b; )
		{
			if( i < 2 )
			{
				i++;
			}
			else if( 0 == i % 1000 && i+1000 <= b )
			{
				re += can[i/1000];

				i+=1000;
			}
			else
			{
				if(n[i])re++;

				i++;
			}
		}

		printf("%d\n",re);
	}

	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