#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int v[200],a[20],prime[100],n,cas=0,num;
void prepare()
{
memset(v,0,sizeof(v));
num=1;
for(int i=2; i<=100; i++)
{
if(!v[i]) prime[num++]=i;
for(int j=1; j<=num&&i*prime[j]<=100; j++)
{
v[i*prime[j]]=true;
if(!(i%prime[j])) break;
}
}
}
int check(int num)
{
// cout<<num<<endl;
for(int i=2; i<=floor(sqrt(num)); i++)
if(num%i==0) return 0;
return 1;
}
void solve(int dep)
{
if(dep==n&&check(a[0]+a[n-1]))
{
for(int i=0; i<n-1; i++) printf("%d ",a[i]);
printf("%d\n",a[n-1]);
}
else
for(int i=2; i<=n; i++)
{
// cout<<"jflsdj"<<endl;
if((!v[i])&&check(a[dep-1]+i))
{
// cout<<"dsfkj"<<endl;
a[dep]=i;
v[i]=true;
solve(dep+1);
v[i]=false;
a[dep]=0;
}
}
}
int main()
{
//prepare();
/*for(int i=1;i<=num;i++)
cout<<prime[i]<<endl;*/
while(scanf("%d",&n)!=EOF)
{
cas++;
memset(v,0,sizeof(v));
if(cas!=1) puts("");
printf("Case %d:\n",cas);
memset(a,0,sizeof(a));
a[0]=1;
v[1]=true;
/*for(int i=0;i<=n;i++)
cout<<i<<" "<<v[i]<<endl;*/
solve(1);
}
return 0;
}
题目地址:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=465
UVA 524(p194)----Prime Ring Problem
最新推荐文章于 2023-10-10 21:15:51 发布