现在的位置: 首页 > 综合 > 正文

HDU 5104 Primes Problem

2018年01月19日 ⁄ 综合 ⁄ 共 837字 ⁄ 字号 评论关闭

Primes Problem

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

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
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer
n(n10000).
Output
For each test case, print the number of ways.
Sample Input
3 9
Sample Output
0 2

//三维化为二维 
#include<iostream>
#include<stdio.h>
#define M 10001
using namespace std;
int prime[M];
int k;

void getPrime()
{
	int i,j;
	memset(prime,0,sizeof(prime));//0表示素数 
	for(i=2;i<M;i++)
	{
		if(!prime[i])
		{
			for(j=2*i;j<M;j+=i)
				prime[j]=1;	
		}
	}
}

int main(){
	int i,j,z,n,count;
	getPrime();
	while(scanf("%d",&n)!=EOF)
	{
		count=0;
        for(i=2;i<=n/3;i++)
        	for(j=i;j<=(n-i)/2;j++)
        		if(!prime[i]&&!prime[j]&&!prime[n-i-j])
        			count++;
		printf("%d\n",count);	
	}
	return 0;
}

抱歉!评论已关闭.