ZOJ 1457 E-Prime Ring Problem
https://vjudge.net/contest/67836#problem/E
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.
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.
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4
Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
时间复杂度:$O(n!)$
题解:dfs 要先判断 N ,如果 N 是奇数的情况下不可能构成素数环,而且如果不先排除会报超时
代码:
#include <bits/stdc++.h>
using namespace std; int N;
int vis[30];
int a[30]; int prime(int x) {
for(int i = 2; i * i <= x; i ++)
if(x % i == 0)
return 0;
return 1;
} void dfs(int step) {
if(step == N + 1 && prime(a[1] + a[N])) {
for(int i = 1; i <= N; i ++) {
printf("%d", a[i]);
printf("%s", i != N ? " " : "\n");
}
return ;
}
for(int i = 2; i <= N; i ++) {
if(vis[i] == 0) {
if(prime(i + a[step - 1])) {
vis[i] = 1;
a[step] = i;
dfs(step + 1);
vis[i] = 0;
}
}
}
return ;
} int main() {
int cnt = 0;
while(~scanf("%d", &N)) {
memset(vis, 0, sizeof(vis));
memset(a, 0, sizeof(a));
printf("Case %d:\n", ++cnt);
if(N % 2) {
printf("\n");
continue;
}
a[1] = 1;
vis[1] = 1;
dfs(2);
printf("\n");
}
return 0;
}
ZOJ 1457 E-Prime Ring Problem的更多相关文章
- ZOJ 1457 Prime Ring Problem(dfs+剪枝)
Prime Ring Problem Time Limit: 10 Seconds Memory Limit: 32768 KB A ring is compose of n circ ...
- UVA524 素数环 Prime Ring Problem
题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016: https://vjudge.net/problem/HDU-10 ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
随机推荐
- while 循环,格式化输出和运算编码
今日内容 1.while循环 while Ture: content = input ("请输入你要喷的内容",输入Q退出) if ...
- python--模块之os操作文件模块
作用:OS又名为:操作系统.所以就是操作系统相关的功能.可以处理文件和目录这些我们日常手动需要做的操作,比如:显示当前目录下所有文件.删除某个文件.获取文件大小...os模块是与操作系统交互的一个接口 ...
- linux驱动动态与静态加载
在Linux中驱动的加载方式有动态加载和静态加载.动态加载,即驱动不添加到内核中,在内核启动完成后,仅在用到这一驱动时才会进行加载静态加载,驱动编译进内核中,随内核的启动而完成驱动的加载.添加字符驱动 ...
- while do while switch语句的简要分析
1 //// while是C语言的一个关键字,其后是使用一个小括号中的条件表达式来做为执行循环的条件, 2 ////也就是说当条件表达式的结果为真时执行大括号里面的的程序内容, 3 ////而当条件表 ...
- LeetCode初级算法的Python实现--动态规划
动态规划的本质是递归:所以做题之前一定要会递归:递归式就是状态转移方程:这里将会介绍使用动态规划做题的思维方式. 统一的做题步骤: 1.用递归的方式写出代码:(此方法写的代码在leetcode中一定会 ...
- mybatis入门(一):jdbc的缺点
mybatis的基础内容 1.mybatis的框架原理 2.mybatis开发dao两种方法: a.原始dao开发方法(程序需要编写dao接口和dao实现类) b.mybatis的mapper接口(相 ...
- java 程序文本文档形式的编写,编译,及运行
一.程序的编写 1.在指定路径下新建文本文档 如在f盘新建了一个名为demo的文件夹,在该文件夹路径下新建了一个文本文档 2.打开文本文档,进行编写,例如: 3.保存 选择文件另存为,文件名称为你创建 ...
- 初识主席树_Prefix XOR
主席树刚接触觉得超强,根本看不懂,看了几位dalao的代码后终于理解了主席树. 先看一道例题:传送门 题目大意: 假设我们预处理出了每个数满足条件的最右边界. 先考虑暴力做法,直接对x~y区间暴枚,求 ...
- 成都Uber优步司机奖励政策(3月30日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 成都Uber优步司机奖励政策(1月15日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...