HDU 5104 BestCoder #18 1001 Primes Problem 水题

题意为对于每个n,求有多少种组合(a,b,c),a<=b<=c,a、b、c都是素数

事先打个1-10000的素数表,然后穷举a和b判断c是否为素数即可

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
using namespace std;
#define LL long long
#define ULL unsigned long long
//#define mod 1000000007
#define eps 1e-8
#define MP make_pair
#define REP(i,a,b) for(int i = a; i < b; ++i)
#define RREP(i,a,b) for(int i = b; i > a ; --i)
#define RE freopen("in.txt","r",stdin)
//#define WE freopen("out.txt","w",stdout)
const int maxn = 10000;
int notprime[maxn+1];
int prime[maxn+1];
int cnt = 0;
void init()
{
memset(notprime,0,sizeof(prime));
notprime[0] = notprime[1] = true;
for(int i = 2; i < maxn; i++)
{
if(!notprime[i])
{
if(i > maxn / i) continue;
for(int j = i * i; j < maxn; j+=i)
notprime[j] = 1;
}
}
}
void getPrime()
{
for(int i = 2; i < maxn; i++)
{
if(!notprime[i])
{
prime[cnt++] = i;
}
}
}
int main()
{
init();
getPrime();
int n;
while(cin>>n)
{
int ans = 0;
for(int i = 0;prime[i] <= n / 3;i++)
for(int j = i ;n-prime[i]-prime[j]>=prime[j];j++)
{
int tmp = n - prime[i] - prime[j];
if(!notprime[tmp])
{
ans++;
}
}
cout<<ans<<endl;
}
}