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:Time Limit Exceed

Posted by fantasyorg at 2008-03-19 00:04:18 on Problem 2909
In Reply To:Time Limit Exceed Posted by:gothree at 2006-11-15 20:39:02
#include<iostream>
using namespace std;

const int size = 40000;
int a[size];
int n;

void getPrime();
 
int solve();

int main()
{
	getPrime();
	 
	while ( true )
	{
		cin >> n;

		if ( n ==  0 )
			break;

		cout << solve() << endl;
	}
	return 0;
}

void getPrime()
{
	for ( int i = 2; i < size; i ++)
		a[i] = 1;

	for ( int j = 2; j < size; j ++ )
	{
		if (a[j])
			for ( int k = j + j; k < size; k += j )
				a[ k ] = 0;
	}
}

int solve()
{
	int c = 0;
	for ( int i = 2; i <= n / 2; i ++ )
		if ( a[i] && a[ n - i ])
			c ++;

	return c;
}

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