You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1015 B
C++

#include <cstdio>
#include <cstring>
using namespace std;
bool notPrime[23], used[23];
int n, T;
int a[23];
void dfs(int id)
{
if (id == n - 1)
for (int i = 0; i < n && !notPrime[a[0] + a[n - 1]]; i++)
printf("%d%c", a[i], " \n"[i == n - 1]);
else
for (int i = 1; i <= n; i++)
if (!used[i] && !notPrime[a[id] + i])
{
used[i] = true;
a[id + 1] = i;
dfs(id + 1);
a[id + 1] = 0;
used[i] = false;
}
}
int main()
{
notPrime[0] = notPrime[1] = true;
for (int i = 0; i < 23; i++)
if (!notPrime[i])
for (int j = i * i; j < 23; j += i)
notPrime[j] = true;
while (~scanf("%d", &n))
{
memset(used, 0, sizeof(used));
memset(a, 0, sizeof(a));
used[a[0] = 1] = true;
printf("Case %d:\n", ++T);
dfs(0);
putchar('\n');
}
return 0;
}