Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 40069 Accepted Submission(s): 17675

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.

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

[hdu1016](http://acm.hdu.edu.cn/showproblem.php?pid=1016)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cmath>
#include <cstring>
using namespace std; #define mem(a) memset(a, 0, sizeof(a))
int a[45], vis[45], vis1[45];
int n; void isprime() {
vis[1] = 2;
for (int i = 2; i<=43; i++) {
if (!vis[i]) vis[i] = 1;
for (int j = 2*i; j<=43; j+=i) vis[j] = 2;
}
} void dfs(int num) {
if (num == n && vis[a[0]+a[num-1]] == 1) {
for (int i = 0; i<num-1; i++) printf("%d ",a[i]);
printf("%d\n",a[num-1]);
}
else {
for (int i = 2; i<=n; i++) {
if (vis1[i] == 0) {
if (vis[i+a[num-1]] == 1) {
vis1[i] = 1;
a[num++] = i;
dfs(num);
num -- ; //回溯
vis1[i] = 0;
}
}
}
}
} int main() {
int f = 1;
isprime();
//for (int i = 1; i<20; i++) cout << vis[i] << endl;
while (scanf("%d",&n) != EOF) {
printf("Case %d:\n",f++);
mem(vis1); mem(a);
a[0] = 1;
dfs(1);
printf("\n");
}
}

HDU1016(素数环)的更多相关文章

  1. HDU1016 素数环---(dfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 Sample Input 6 8   Sample Output Case 1: 1 4 3 2 5 6 ...

  2. 素数环问题[XDU1010]

    Problem 1010 - 素数环问题 Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty: Total Submit: 972  Acc ...

  3. Prime Ring Problem + nyoj 素数环 + Oil Deposits + Red and Black

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

  4. nyist 488 素数环(搜索+回溯)

     素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 有一个整数n,把从1到n的数字无反复的排列成环,且使每相邻两个数(包含首尾)的和都为素数,称为素数环. ...

  5. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  6. 【DFS】素数环问题

    题目: 输入正整数n,对1-n进行排列,使得相邻两个数之和均为素数,输出时从整数1开始,逆时针排列.同一个环应恰好输出一次.n<=16 如输入: 6 输出: 1 4 3 2 5 6 1 6 5 ...

  7. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  8. 素数环 南阳acm488(回溯法)

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

  9. UVA 524 素数环 【dfs/回溯法】

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

  10. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

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

随机推荐

  1. c语言文件分割与合并

    一.综述 c语言操作文件通过文件指针FILE*,每个要操作的文件必须打开然后才能读写. 注意事项: @1分割与合并文件最好使用二进制模式即"rb"或"wb",这 ...

  2. ArcGIS API for JavaScript 中的数据类型【vs】GPServer的数据类型

    熟悉GPServer的同学肯定知道,GPServer在10.1的ArcMap后需要执行成功一次才能发布. 发布GPServer,可以是ArcMap的工具箱的工具,也可以是自己写的模型. 不管是ArcM ...

  3. 开启tomcat的apr模式,并利用redis做tomcat7的session的共享。

    更新系统组件 yum -y install readline* xmlto kernel-devel yum* screen vim* psmisc wget lrzsz pcre-devel lib ...

  4. UVA 11825 Hackers' Crackdown

    题目大意就是有一个图,破坏一个点同时可以破坏掉相邻点.每个点可以破坏一次,问可以完整破坏几次,点数=16. 看到16就想到状压什么的. 尝试设状态:用f[i]表示选的情况是i(一个二进制串),至少可以 ...

  5. JFinal极速开发框架使用笔记

    记录第一次使用JFinal,从简单的框架搭建到增删改查,从自带的方法到正常框架习惯的使用方式. JFinal官网:http://www.jfinal.com/ JFinal 是基于 Java 语言的极 ...

  6. SQL企业级面试题

    链接:90root MySQL企业面试题 1. 开发有一堆数据插入,如何防止插入的中文数据产生乱码? 2. 如何批量更改数据库表的引擎,如:myisam改为innodb 3. 如何批量更改数据库字符集 ...

  7. AngularJS 模板

    一个应用的代码架构有很多种.对于AngularJS应用,我们鼓励使用模型-视图-控制器(MVC)模式解耦代码和分离关注点.考虑到这一点,我们用AngularJS来为我们的应用添加一些模型.视图和控制器 ...

  8. centOS7 mini配置linux服务器(四) 配置jdk

    这里简单写一下centos7Mini  安装jdk1.8的全过程. 一.下载jdk,linux版本. 地址:http://www.oracle.com/technetwork/java/javase/ ...

  9. haslayout知多少

    我们都知道浏览器有bug,而IE的bug似乎比大多数浏览器都多.IE的表现与其他浏览器不同的原因之一就是,显示引擎使用一个称为布局(layout)的内部概念.   因为布局是专门针对显示引擎内部工作方 ...

  10. oracle 处理时间和金额大小写的相关函数集合

    CREATE OR REPLACE FUNCTION MONEY_TO_CHINESE(MONEY IN VARCHAR2) RETURN VARCHAR2 IS C_MONEY ); M_STRIN ...