-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUVA-524.cpp
More file actions
46 lines (40 loc) · 952 Bytes
/
UVA-524.cpp
File metadata and controls
46 lines (40 loc) · 952 Bytes
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 <bits/stdc++.h>
using namespace std;
int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41};
int n, c = 0;
vector<int> resp(20, 1);
bool isPrime(int x){
for (int p : primes)
if (p == x) return true;
return false;
}
void backtracking(int l, vector<bool> visitados)
{
if (l == n) {
if (!isPrime(resp[n-1] + 1))
return;
cout << "1";
for (int i = 1; i < n; i++)
cout << " " << resp[i];
cout << endl;
return;
}
for (int i = 2; i <= n; i++) {
if (visitados[i]) continue;
if (isPrime(i + resp[l - 1])) {
visitados[i] = 1;
resp[l] = i;
backtracking(l + 1, visitados);
visitados[i] = 0;
}
}
}
int main()
{
while (cin >> n) {
if (c++) cout << endl;
cout << "Case " << c << ":\n";
vector<bool> visitados(20, 0);
backtracking(1, visitados);
}
}