Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
万恶的sqrt(int)又害我CE一次,POJ的编译器要更新了!!!!#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator