A ring is composed of n (even number) circles as shown in diagram. Put natural numbers 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 ≤16)

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.

You are to write a program that completes above process.

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

HINT

回溯法解决,直接无脑暴力破解工作量巨大。结合使用深度优先遍历递归,每次递归完成之后将其恢复原状,大大节省时间。另外,由于n<=16,最大的和素数为31,直接枚举出来即可。

Accepted

#include<iostream>
#include<algorithm>
#include<cstring>
#include<set>
#include<vector>
using namespace std;
int n, Case = 0;
vector<int>list; int vis[17] = { 0 };
set<int>prime_number = { 2,3,5,7,11,13,17,19,23,29,31 };
void dfs(int num) {
if (num == n && prime_number.count(list.front() + list.back()))
for (int i = 0; i < n; i++)cout << list[i] << (i == n - 1 ? '\n' : ' ');
else for(int i=2;i<=n;i++)
if (!vis[i] && prime_number.count(i + list.back())) {
list.push_back(i); vis[i] = 1;
dfs(num + 1);
list.pop_back(); vis[i] = 0;
}
}
int main() {
while (cin >> n) {
cout << (Case == 0 ? "" : "\n");
memset(vis, 0, sizeof(vis));
list.clear();
cout <<"Case " << ++Case <<':'<< endl;
list.push_back(1); vis[1] = 1;
dfs(1);
}
}

Prime Ring Problem UVA - 524的更多相关文章

  1. 暴力求解——素环数 Prime Ring Problem ,UVa 524

    Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers i ...

  2. uva 524(Prime Ring Problem UVA - 524 )

    dfs练习题,我素数打表的时候j=i了,一直没发现实际上是j=i*i,以后可记住了.还有最后一行不能有空格...昏迷了半天 我的代码(紫书上的算法) #include <bits/stdc++. ...

  3. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  4. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  5. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  6. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  7. hdu 1016 Prime Ring Problem(DFS)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  8. HDU 1016 Prime Ring Problem(经典DFS+回溯)

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. 杭电oj 1016 Prime Ring Problem

    Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. 读懂RESTful风格

    RESTful就是资源定位和资源操作的风格.不是标准也不是协议. REST即Representational State Transfer的缩写,可译为"表现层状态转化".REST ...

  2. Oracle 开启或关闭归档

    开启:sqlplus / as sysdbaarchive log list;shutdown immediate;startup mount;alter database archivelog;ar ...

  3. Java基本概念:内部类

    一.简介 描述: 很多时候我们创建类的对象的时候并不需要使用很多次,每次只使用一次,这个时候我们就可以使用内部类了. 内部类不是在一个java源文件中编写两个平行的类,而是在一个类的内部再定义另外一个 ...

  4. Hi3559AV100板载开发系列-pthread_create()下V4L2接口MJPEG像素格式的VIDIOC_DQBUF error问题解决-采用阻塞方式下select监听

     最近一直加班加点进行基于Hi3559AV100平台的BOXER-8410AI板载开发,在开发的过程中,遇到了相当多的问题,其一是板载的开发资料没有且功能不完整,厂家不提供太多售后技术支持,厂家对部分 ...

  5. 有钱人买钻石+dfs中使用贪心

    有钱人买钻石 ECNU-3306 题解:这个题目,乍一看以为是dp背包,可是数据量却那么大,只有1,5,10,25四种面额的硬币,每种数量若干,要使得能够刚好兑换成功总金额,在此前提下,还要使得硬币数 ...

  6. 在 .NET 中使用 Flurl 高效处理Http请求

    简介 官方介绍,Flurl是一个现代的,流利的,支持异步的,可测试的,可移植的,URL增强和Http客户端组件. Url构建 现在有一个登录的接口,地址如下: https://www.some-api ...

  7. C# 基础 - Enum 的一些操作

    1. int 转换成 enum public enum Suit { Spades, Hearts, Clubs, Diamonds } Suit spades = (Suit)0; Suit hea ...

  8. Java数组:初识数组

    数组:数组是相同类型数据的有序集合数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成其中,每一个数据称作一个数组元素,每个数组元素可以通过一个下标来访问他们 数组基本特点:其长度是确定的 ...

  9. JavaScript数组的几个常用的API

    一. 扁平化嵌套数组/展平和阵列孔--flat() 1.实现效果 var arr1 = [1, 2, [3, 4]]; arr1.flat(); // [1, 2, 3, 4] var arr2 = ...

  10. 基金 A 类和 C 类、ETF、LOF、QDII 到底是啥?

    ETF 对于初入股市的新手来说,买了一只公司股票容易,想买一个行业的股票就不是很容易了. 比如你要懂得行业里都有谁,每个公司分配多少钱,最主要股票交易最少要交易 1 手也就是 100 股,要是想配置一 ...