-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP524.cpp
74 lines (68 loc) · 1.7 KB
/
P524.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
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
#include <iostream>
struct PermutationNode {
PermutationNode *next;
int val;
};
struct PermutationHandler {
PermutationNode *nodes;
PermutationHandler(int size) {
nodes = new PermutationNode[size+1];
for(int i = 0; i < size; ++i) {
nodes[i+1].val = i+2;
nodes[i].next = &(nodes[i+1]);
}
nodes[size].next = NULL;
}
~PermutationHandler() {
delete[] nodes;
}
PermutationNode* root() {
return &(nodes[0]);
}
};
void findCircles(PermutationHandler &ph, int *perm, const int i,
const int len, bool const * const primes) {
if(i == len) {
if(primes[1+perm[len-1]]) {
std::cout << "1";
for(int j = 1; j < len; ++j)
std::cout << " " << perm[j];
std::cout << std::endl;
}
return;
}
// try all combinations:
PermutationNode *node = ph.root();
while(node->next != NULL) {
PermutationNode *n = node->next;
// remove n from permutation:
node->next = n->next;
perm[i] = n->val;
if(primes[perm[i-1]+n->val]) { // is prime.
findCircles(ph, perm, i+1, len, primes);
}
// re-insert in permutation and go to next:
node->next = n; // n->next is already set (never changes)
node = n;
}
}
int main() {
bool primes[32] = {0,0,1,1,0,1,0,1,0,0, // 0-9
0,1,0,1,0,0,0,1,0,1, // 10-19
0,0,0,1,0,0,0,0,0,1, // 20-29
0,1}; // 30-31
int permutation[16];
permutation[0] = 1;
int n;
for(int cas = 1; std::cin >> n; ++cas) {
if(cas != 1)
std::cout << std::endl;
std::cout << "Case " << cas << ":" << std::endl;
if(n == 1) {
std::cout << "1" << std::endl;
continue;
}
PermutationHandler ph(n-1);
findCircles(ph, permutation, 1, n, primes);
}
}