#include<iostream>
#include<string>
#include<cassert>
#include<algorithm>
#include<map>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<vector>
#include<set>
using namespace std;
bool is_prime(int x)
{
if (x == 2)
return true;
for (int i = 2; i <= sqrt(x) + 1; ++i)
{
if (x % i == 0)
return false;
}
return true;
}
const int maxn = 16 + 2;
int A[maxn];
int vis[maxn];
int n;
void print_ans(int A[])
{
for (int i = 1; i <= n; ++i)
{
if (i != 1)
cout << ' ';
cout << A[i];
}
cout << endl;
}
void dfs(int cur)
{
if (cur > n && is_prime(A[n] + A[1]))
print_ans(A);
for (int i = 2; i <= n; ++i)
{
if (is_prime(A[cur - 1] + i) && !vis[i])
{
A[cur] = i;
vis[i] = 1;
dfs(cur + 1);
vis[i] = 0;
}
}
}
int main()
{
//freopen("input.txt", "r", stdin);// freopen("output2.txt", "w", stdout);
int kase = 0;
while (cin >> n)
{
if (kase)cout << endl;
cout << "Case " << ++kase << ":\n";
memset(A, 0, sizeof(A));
memset(vis, 0, sizeof(vis));
vis[1] = 1;
A[1] = 1;
dfs(2);
}
}
紫书素数环UVa524
最新推荐文章于 2024-09-22 11:42:19 发布