# include<stdio.h>
# include<algorithm>
# include<string.h>
using namespace std;
int vis[25];
int num[25];
int n,cot;
int prime(int sum)
{
int i;
if(sum==2)
return false;
for(i=2;i*i<=sum;i++)
{
if(sum%i==0)
break;
}
if(i*i>sum)
return true;
return false;
}
void dfs()
{
int i;
for(i=1;i<=n&&cot<=n;i++)
{
if(!vis[i]&&prime(num[cot-1]+i))
{
vis[i]=1;
num[cot++]=i;
dfs();
vis[num[cot-1]]=0;
cot--;
}
}
if(cot==n+1&&prime(num[1]+num[cot-1]))
{
for(i=1;i<=n;i++)
{
if(i==1)
printf("%d",num[i]);
else
printf(" %d",num[i]);
}
printf("\n");
}
}
int main ()
{
int cas=0;
while(~scanf("%d",&n))
{
printf("Case %d:\n",++cas);
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
cot=2;
vis[1]=1;
num[1]=1;
dfs();
printf("\n");
}
return 0;
}
hdu 1016(经典搜素)
最新推荐文章于 2021-02-26 15:30:16 发布