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,郁闷!!!

Posted by kikif at 2007-09-06 15:17:08 on Problem 3006
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;

int prime[500000];
int pn;


void init(){
	pn=1;
	prime[0]=2;
	int i=3,j=0;
	for(i=3;i<=100000;i++){
		bool find=false;
		for(j=0;prime[j]<=sqrt(i)&&j<pn;j++){
			if(i%prime[j]==0){
				find=true;
				break;
			}
		}
		if(!find){
			prime[pn++]=i;
		}
	}
}

bool binarysearch(int p){
	int i=0,j=pn-1;
	while(i<=j){
		int mid=(i+j)/2;
		if(prime[mid]==p)return true;
		else if(prime[mid]<p){
			i=mid+1;
		}else{
			j=mid-1;
		}
	}
	return false;
}


void main(){
//	ifstream cin("data.txt");
	init();
	while(1){
		int a,d,n,i,j,k;
		cin>>a>>d>>n;
		if(a==0&&d==0)break;
		k=a;
		int count=0;
	//	if(binarysearch(a))n++;
		for(i=0;;i++){
			int cur=a+d*i;
			if(binarysearch(cur))count++;
			if(count==n)break;
		}
		cout<<a+d*i<<endl;
	}
}

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