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

又忘了\n怒贡献一次PE。。。

Posted by KatrineYang at 2016-11-12 14:47:05 on Problem 1595
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
using namespace std;
int primes[1229];
int cnt = 2;

int MX(int a, int b){return (a>b)?a:b;}
int MN(int a, int b){return (a<b)?a:b;}

void getPrime(int N){
	//int cnt = 2;
	primes[0] = 1, primes[1] = 2;
	for(int i = 3; i < N; i+=2){
		int q = (int)sqrt(i+0.0);
		bool ss = 1;
		for(int j = 2; j<cnt && primes[j]<= q; j++){
			if(i%primes[j]==0){
				ss=0; break;
			}
		}
		if(ss){
			primes[cnt] = i; cnt++;
		}
	}
}

int findIdx(int N){
	int l = 0, u = cnt-1;
	while(l<u){
		int m = (l+u+1)/2;
		if(primes[m] <= N){
			l = m;
		}
		else{
			u = m-1;
		}
	}
	return l;
}

int main() {
	getPrime(1100);
	int N,C;
	while(scanf("%d%d", &N, &C) > 0){
		int mx = findIdx(N);
		int l,u;
		if(mx%2==0){
			//jishuge
			l = MX(0, mx/2-(C-1)), u = MN(mx, mx/2+(C-1));
		}
		else{
			//oushuge
			l = MX(0, mx/2-C+1), u = MN(mx, mx/2+C);
		}
		printf("%d %d:", N, C);
		for(int i = l; i <= u; i++) printf(" %d", primes[i]);
		printf("\n");
	}
	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