hdu 1016 Prime Ring Problem
来源:互联网 发布:java获取list泛型类型 编辑:程序博客网 时间:2024/06/11 01:15
Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.
Note: the number of first circle should always be 1.
Note: the number of first circle should always be 1.
Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
You are to write a program that completes above process.
Print a blank line after each case.
Sample Input
68
Sample Output
Case 1:1 4 3 2 5 61 6 5 2 3 4Case 2:1 2 3 8 5 6 7 41 2 5 8 3 4 7 61 4 7 6 5 8 3 21 6 7 4 3 8 5 2
思路:终于考完了冬季“大雪崩”的考试,现在水平也就做做基础题了,本题dfs,用筛法筛出质数然后每次dfs当前的没有被用过数和层数,当转一圈层数正好等于n时再输出,用fa[]记录每个点的父亲结点方便输出,不符合条件的dfs之后要消去访问标记。
#include<iostream>#include<cstring>#define CLR(a,b) memset(a,b,sizeof(a))using namespace std;const int MAXN=2e2+5;int n,phi[MAXN],ans[MAXN],fa[MAXN],vis[MAXN];void eular(){for(int i = 0; i<MAXN;i++) phi[i] = i;for(int i = 2; i<MAXN;i++) if(phi[i] == i) for(int j = i+i;j<MAXN;j+=i) phi[j] = 0;}void print(int cur){int n=0;while(cur!=1){ans[n++]=cur;cur=fa[cur];}cout<<1;for(int i=n-1;i>=0;i--)cout<<' '<<ans[i];cout<<endl;}void dfs(int cur,int k){if(k==n && phi[cur+1]){print(cur);}//cout<<cur<<' '<<k<<endl;for(int i=2;i<=n;i++){if(phi[cur+i] && !vis[i]){vis[i]=1;fa[i]=cur; dfs(i,k+1);vis[i]=0;}}}void init(){CLR(ans,0);CLR(fa,0);CLR(vis,0);}int main(){int ca=1;eular();while(cin>>n){cout<<"Case "<<ca++<<":"<<endl;init();dfs(1,1);cout<<endl;}return 0;}
0 0
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 prime ring problem
- hdu 1016Prime Ring Problem
- hdu 1016 Prime Ring Problem
- HDU-1016 Prime Ring Problem
- HDU-1016-Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 Prime Ring Problem
- hdu 1016 Prime Ring Problem
- Hdu 1016 - Prime Ring Problem
- HDU 1016 Prime Ring Problem
- PerformanceCounter 性能计数器的使用
- 关于设计模式的思考——创建型模式
- c#Com控件开发
- FZU 1054阅读顺序
- Android应用图标替换
- hdu 1016 Prime Ring Problem
- redis列表键的底层实现之链表
- 欢迎使用CSDN-markdown编辑器
- php7/mysql/apache/windows环境搭建
- 总结一下HBase各种级别的锁以及对读写的阻塞
- [leetcode] 109. Convert Sorted List to Binary Search Tree
- Codeforces 27B Tournament(dfs,判断有向图中点的连通性)
- lua 实现类继承和覆盖
- 本地虚拟机Ubuntu14.04系统和宿主机Windows系统通信问题(java编写的socket通信)