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

万恶的sqrt(int)又害我CE一次,POJ的编译器要更新了!!!!

Posted by KatrineYang at 2016-11-16 11:03:06 on Problem 3978
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <cmath>

using namespace std;

int primeList[20000];
int primeCnt = 0;

void getPrime(int thres){
	primeList[0] = 2;
	primeCnt = 1;
	for(int i = 3; i < thres; i+=2){
		bool isPrime = 1;
		int sq = (int)sqrt(i);
		for(int j = 0; j < primeCnt; j++){
			if(primeList[j] > sq) break;
			if(i%primeList[j] == 0){
				isPrime = 0;break;
			}
		}
		if(isPrime){
			primeList[primeCnt] = i;
			primeCnt++;
		}
	}
	primeList[primeCnt] = 0;
}


int getPrimeIndex(int n){
	if(n<2) return 0;
	if(n>=primeList[primeCnt-1]) return primeCnt;
	int l = 0, u = primeCnt-1;
	while(l < u){
		int m = (l+u)/2;
		if(primeList[m] > n) u = m;
		else l = m+1;
	}
	return l;
}

int main(int argc, char **argv){
	getPrime(100000);
	int A, B;
	while(1){
		scanf("%d%d",&A,&B);
		if(A==-1 && B==-1) break;
		int ap = getPrimeIndex(A), bp = getPrimeIndex(B);
		int res = bp - ap;
		if(A >= 2 && primeList[ap-1] == A) res++;
		printf("%d\n", res);
	}
	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