hdu 1016 Prime Ring Problem
来源:互联网 发布:c语言malloc函数用法 编辑:程序博客网 时间:2024/06/15 14:44
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17618 Accepted Submission(s): 7937
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.
![](http://acm.hdu.edu.cn/data/images/1016-1.gif)
Note: the number of first circle should always be 1.
![](http://acm.hdu.edu.cn/data/images/1016-1.gif)
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生成素数环,用dfsAC代码:#include <iostream>#include <cstring>#include <cmath>using namespace std;int vis[25],a[25],vis_p[60],n;void dfs(int cur){ if(cur==n&&!vis_p[a[0]+a[n-1]]) { for(int i=0;i<n-1;i++) cout<<a[i]<<" "; //最后一个数后面不能输出空格 cout<<a[n-1]; cout<<endl; } else for(int i=2;i<=n;i++) { if(!vis[i]&&!vis_p[i+a[cur-1]]) { a[cur]=i; vis[i]=1; dfs(cur+1); vis[i]=0; } }}int main(){ int m = sqrt(50); memset(vis_p,0,sizeof(vis_p)); for(int i=2;i<m;i++) if(!vis_p[i]) for(int j=i*i;j<=50;j+=i) vis_p[j]=1; int c=0; a[0]=1; while(cin>>n) { memset(vis,0,sizeof(vis)); c++; cout<<"Case "<<c<<":"<<endl; dfs(1); cout<<endl; } return 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
- SOCK5代理服务的设计与实现
- SFX: constructing constants
- 粒子系统
- 写给3年后的XXX【连载】--写作中的感慨
- Servlet生命周期
- hdu 1016 Prime Ring Problem
- Android 内存溢出解决方案(OOM) 整理总结
- recv出现乱码后——对recv函数的一点思考和要注意的问题
- 【程序50】#include 的应用练习
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- 线程状态转换图
- Boost.Asio技术文档
- android支付平台集成调研
- 三向切分快排