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 dinysirius at 2009-03-03 13:47:50 on Problem 3006
典型空间换时间的算法

#include <iostream>
#include <memory.h>
using namespace std;

const int max = 1000005;

bool isprime[::max];

int main(){

	int i,j;
	
	memset(isprime,true,sizeof(isprime));
	
	for(i = 3 ; i <= 1000 ; i += 2 ){
		
		for(j = 3 ; j <= ::max / i ; j += 2){
		
			if(isprime[i]){
			
				isprime[i * j] = false;	
			}	
		}	
	}
	
	for(i = 4 ; i <= ::max ; i += 2 ){
		
		isprime[i] = false;	
	}
	isprime[1] = isprime[0] = false;
	
	int a,d,n;
	
	while(cin >> a >> d >> n && !(!a && !d && !n)){
		
		int num[250] = {0};
		
		int j = 1;
		
		for(int i = a ; j <= n ; i += d){
			
			if(isprime[i]){
				
				num[j++] = i;	
			}	
		}	
		
		cout << num[n] << 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