-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUVA00524.cpp
46 lines (43 loc) · 851 Bytes
/
UVA00524.cpp
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
#include <cstdio>
#include <unordered_set>
using namespace std;
unordered_set<int> primes({2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37});
int sol[16], n;
bool used[17];
void proc(int pos) {
if(pos == n) {
if (primes.count(sol[n-1] + sol[0])) {
bool first = false;
for (int i = 0; i < n; i++) {
if (first) printf(" ");
else first = true;
printf("%d", sol[i]);
}
printf("\n");
}
} else {
for (int i = 2; i <= n; i++) {
if (!used[i] && primes.count(sol[pos-1] + i)) {
used[i] = true;
sol[pos] = i;
proc(pos+1);
used[i] = false;
}
}
}
}
int main() {
int cases = 1;
sol[0] = 1;
for (int i = 2; i <= n; i++) {
used[i] = false;
}
bool first = false;
while (scanf("%d", &n) != EOF) {
if (first) printf("\n");
else first = true;
printf("Case %d:\n", cases++);
proc(1);
}
return 0;
}