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

太简单了,太烦了,贴一下下。。。

Posted by zihuacs at 2010-08-19 11:52:00 on Problem 1595
#include<iostream>
using namespace std;
bool prime[1001];
int count[1001];
int n,c;
inline void Init(void)
{
	int i,j;
	memset(prime,true,sizeof(prime));
	prime[1]=true;
	for(i=2; i<33; i++)
	{
		if(prime[i])
		{
			for(j=2*i; j<1001; j+=i)
			{
				prime[j]=false;
			}
		}
	}
	count[0]=0;
	for(i=1; i<1001; i++)
	{
		count[i]=count[i-1] + (prime[i]==true ? 1: 0);
	}
}
// 1 2 3 5 7 11 13 17 19   
// 1.奇数 2*c-1   起点 = a+a+2*c-1=len 2*a=len+1-2*c a=(len+1)/2-c  
// 2.偶数 2*c     起点 = a+a+2*c=len   2*a=len-2*c   a=(len  )/2-c
//
inline void Solve(void)   // 用inline
{
	int a;
	int i;
	a=(count[n]+(count[n] & 1))/2 - c;
	if(a<=0)
	{
		a=0; c=count[n];
	}
	else
		c=2*c-(count[n]&1);

	for(i=1; a>0 ; i++)
	{
		if(prime[i])
			a--;
	}
	for(; c>0 ; i++)
	{
		if(prime[i])
		{
			c--;
			printf("%d ",i);
		}
	}
	printf("\n\n");

}
int main(void)
{
	int i;
	while(scanf("%d %d",&n,&c)==2)
	{
		Init();
		printf("%d %d: ",n,c);
		Solve();
	}
	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