1031 质数环
1031 質數環
?
時間限制: 1 s 空間限制: 128000 KB 題目等級 : 黃金 Gold 題目描述 Description一個大小為N(N<=17)的質數環是由1到N共N個自然數組成的一個數環,數環上每兩個相鄰的數字之和為質數。如下圖是一個大小為6的質數環。為了方便描述,規定數環上的第一個數字總是1。如下圖可用1 4 3 2 5 6來描述。若兩個質數環,數字排列順序相同則視為本質相同。現在要求你求出所有本質不同的數環。
輸入描述 Input Description
只有一個數N,表示需求的質數環的大小。如:
每一行描述一個數環,如果有多組解,按照字典序從小到大輸出。如:
樣例輸入 Sample Input6
樣例輸出 Sample Output1 4 3 2 5 6
1 6 5 2 3 4
數據范圍及提示 Data Size & Hint n<=17 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 const int N=18; 6 int n; 7 int vis[N]; 8 int ans[N]; 9 int top; 10 int use[N]; 11 void print() 12 { 13 for(int i=1;i<=n;i++) 14 { 15 printf("%d ",ans[i]); 16 } 17 printf("\n"); 18 } 19 bool pd(int l) 20 { 21 for(int i=2; i<=sqrt(l); i++) 22 if(l%i==0)return false; 23 return true; 24 } 25 void dfs(int x) 26 { 27 for(int i=2;i<=n;i++) 28 { 29 if(use[i]==0&&pd(ans[x-1]+i)) 30 { 31 ans[x]=i; 32 use[i]=1; 33 if(x==n&&pd(ans[n]+ans[1]))print(); 34 else dfs(x+1); 35 use[i]=0; 36 } 37 } 38 } 39 int main() 40 { 41 cin>>n; 42 if(n%2==1) 43 { 44 cout<<endl; 45 return 0; 46 } 47 ans[1]=1; 48 dfs(2); 49 }?
轉載于:https://www.cnblogs.com/lyqlyq/p/6752731.html
總結
- 上一篇: 通达信l2接口如何获取?
- 下一篇: 联想rs240服务器型号在哪看,【Thi