[HDU 1016]--Prime Ring Problem(回溯)
2015-06-21 19:32
423 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016
Prime Ring Problem
[b]Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
[/b]
[align=left]Problem Description[/align]
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.
View Code
Prime Ring Problem
[b]Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
[/b]
[align=left]Problem Description[/align]
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.
#include <iostream> #include <cstring> using namespace std; #define maxn 40 int vis[21], x[21], T, n; int prime[maxn] = { 1, 1, 0 }; void init() { int i, j; for (i = 2; i <= maxn; i++){ if (!prime[i]){ for (j = 2; i*j <= maxn; j++) prime[i*j] = 1; } } } void dfs(int cur){ if (cur == n&&!prime[1 + x[n - 1]]){ for (int i = 0; i < n; i++){ if (i) cout << ' '; cout << x[i]; } cout << endl; } else for (int i = 2; i <= n; i++){ if (!vis[i] && !prime[i + x[cur - 1]]){ x[cur] = i; vis[i] = 1; dfs(cur + 1); vis[i] = 0; } } } int main(){ init(); while (cin >> n){ cout << "Case " << ++T << ':' << endl; if (n == 1) cout << 1 << endl; else if (n & 1) cout << endl; else{ memset(vis, 0, sizeof(vis)); x[0] = 1; dfs(1); } cout << endl;//没加这一句pe来个wa,我也是醉了,各种改,无爱了~~~~ } return 0; }
View Code
相关文章推荐
- oc 的优缺点
- 【牛腩】div+css模型
- javascript:深入理解js闭包
- 我看我自己系列——我毁掉的生活我痛苦的地方。
- Count and Say
- SimpleDateFormat使用特定的解释
- POJ 2481 Cows【树状数组】
- SQL like使用 模糊查询
- ubuntu系统学习之:64位ubuntu系统安装32位兼容库ia32-libs的方法
- 黑马程序员--Java学习日记之面试问题汇总,感谢黑马论坛!
- bootstrap折叠内容
- AndroidStudio中百度地图接口使用配置相关总结
- 解决android studio ADB无响应的问题:If you'd like to retry,then please manually kill "adb.exe"
- 运行及总结
- java csv 0开头问题
- static 关键字的作用
- activiti 第一篇:整合activiti 5.17.0 进项目
- Eclipse J2EE+Maven+Tomcat(Jetty)构建Spring MVC项目
- python一些内置函数——filter、map、reduce
- 小问题---要养成好的编码习惯