0
点赞
收藏
分享

微信扫一扫

hdoj Primes Problem 5104 (素数打表&&技巧)


Primes Problem


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1978    Accepted Submission(s): 911



Problem Description


Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.




Input


n(n≤10000).




Output


For each test case, print the number of ways.




Sample Input


3 9




Sample Output


0 2


 


#include<stdio.h>
#include<string.h>
#include<math.h>
#define N 10010
int p[N];
bool pp[N];
int pnum;
int getp()
{
	int i,j;
	memset(pp,false,sizeof(pp));
	for(i=2;i<N;i++)
	{
		if(!pp[i])
			p[pnum++]=i;
		for(j=0;j<pnum&&p[j]*i<N;j++)
		{
			pp[p[j]*i]=true;
			if(i%p[j]==0)
				break;
		}
	}
	pp[0]=pp[1]=true;
}
int main()
{
	int n,m,i,j,k;	
	getp();
	while(scanf("%d",&n)!=EOF)
	{
		int cnt=0;
		for(i=0;p[i]<n;i++)
		{
			for(j=i;p[j]<n;j++)
			{
				if(!pp[n-p[i]-p[j]]&&(n-p[i]-p[j])>=p[j])
					cnt++;
			}
		}
		printf("%d\n",cnt);
	}
	return 0;
}


举报

相关推荐

0 条评论