#include<iostream>
using namespace std;
int arr[22];
int flag[25];
int ring[22];
int num;
int isPrime(int n){
if (n <= 3) {
return n > 1;
}
for(int i = 2; i < n; i++){
if (n % i == 0) {
return 0;
}
}
return 1;
}
void dfs(int t,int index){
if(index==num){
if(isPrime(arr[0]+arr[t])==1){
for(int i=0;i<num;i++){
if(i==num-1){
cout<<ring[i];
}
else{
cout<<ring[i]<<" ";
}
}
cout<<endl;
}
}
for(int i=0;i<num;i++){
if(flag[(i+1)%num]==0&&isPrime(arr[(i+1)%num]+arr[t])==1){
flag[(i+1)%num]=1;
ring[index]=arr[(i+1)%num];
int temp =index+1;
dfs((i+1)%num,temp);
flag[(i+1)%num]=0;
}
}
}
int main(){
int time=0;
while(cin>>num){
time++;
for(int i=0;i<num;i++){
flag[i]=0;
arr[i]=i+1;
}
flag[0]=1;
ring[0]=1;
cout<<"Case "<<time<<":"<<endl;
dfs(0,1);
cout<<endl;
}
return 0;
}
hdu1016素数环
最新推荐文章于 2024-08-22 10:56:53 发布