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 & % ...
随机推荐
- pyhton实现简单的木马程序
十一的晚上,平时都在写工作的代码,好久没有专门看一些知识了,感觉想刚开始学c一样,搞到半夜 还是<python网络编程基础>,写了小脚本,没有任何结构,一句一句的往下写的,反正是可以实现想 ...
- BAPIを使用のODATA作成
入力: AIRLINE テーブル: FLIGHT_LIST Step 1: TCode: SEGW ⇒新規作成ボタンを押す Step 2: オブジェクト名など入力 Step 3: オブジェクト作成完了 ...
- CF480Div2掉分记
rating 1900+参加只有Div2的比赛也记rating了.还以为yyc报名没打会惨惨,原来不交题好像就不算参加.. 本来太晚了不想打,不过有Sinogi大佬带我还是打一打吧,apio之前练练手 ...
- 初识主席树_Prefix XOR
主席树刚接触觉得超强,根本看不懂,看了几位dalao的代码后终于理解了主席树. 先看一道例题:传送门 题目大意: 假设我们预处理出了每个数满足条件的最右边界. 先考虑暴力做法,直接对x~y区间暴枚,求 ...
- 北京Uber优步司机奖励政策(1月15日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 5、Java并发编程:Lock
Java并发编程:Lock 在上一篇文章中我们讲到了如何使用关键字synchronized来实现同步访问.本文我们继续来探讨这个问题,从Java 5之后,在java.util.concurrent.l ...
- springboot之redis的应用
1.redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set(集合).zset(sorted se ...
- Python3 linux安装
./configure --prefix=/usr/local/python3 --with-ssl --enable-optimizations make && make insta ...
- LWM2M的DISCOVER操作
1. 先看下DISCOVER的数据流,工作服务器下发的指令到设备客户端 2. 解释,这个操作是用来发现Object, Object Instances, and Resources的属性,同时可以发现 ...
- 三 Hive 数据处理 自定义函数UDF和Transform
三 Hive 自定义函数UDF和Transform 开篇提示: 快速链接beeline的方式: ./beeline -u jdbc:hive2://hadoop1:10000 -n hadoop 1 ...