HDU 5104 Primes Problem(素数打表)
Primes Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3614 Accepted Submission(s): 1629
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(n≤10000).
Output
For each test case, print the number of ways.
Sample Input
3
9
Sample Output
0
2
题目不难,就是先打表在暴力,直接暴力不用想肯定超。
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
int pr[10010],bo[100000]={0};
int num=0;
void primes()
{
for (int i=2;i<=10000;i++)
{
if(!bo[i])
pr[++num]=i;
for (int j=1;j<=num && pr[j]*i<=10000;j++)
{
bo[i*pr[j]]=1;
if(i%pr[j]==0)break;
}
}
}
int main()
{
primes();
int k,n,m,sum;
while(scanf("%d",&k)!=EOF)
{
sum=0;
for(int i=1;pr[i]<=k;i++)
{
for(int j=i;pr[j]<=k;j++)
{
if(!bo[k-pr[i]-pr[j]]&&(k-pr[i]-pr[j])>=pr[j])
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}