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.

 

Inputn (0 < n < 20). 
OutputThe 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 思路:依旧是回溯法,逐个判断即可,注意首尾也需要判断一下,用筛法建个表,代码如下:
const int maxm = ;

int n, vis[maxm], prime[], res[maxm], kase = ;

void buildTable() {
for (int i = ; i * i < ; ++i) {
if(!prime[i]) {
for (int j = i * i; j < ; j += i)
prime[j] = ;
}
}
} void dfs(int i) {
if(i == n && !prime[res[] + res[n-]]) {
for (int j = ; j < n; ++j) {
if(j)printf(" ");
printf("%d", res[j]);
}
printf("\n");
}
for (int j = ; j <= n; ++j) {
if(!vis[j] && !prime[res[i-] + j]) {
vis[j] = ;
res[i] = j;
dfs(i + );
vis[j] = ;
}
}
} int main() {
buildTable();
while(scanf("%d",&n) != EOF) {
printf("Case %d:\n", ++kase);
memset(vis, , sizeof(vis));
res[] = ;
vis[] = ;
dfs();
printf("\n");
}
return ;
}

Day2-M-Prime Ring Problem-HDU1016的更多相关文章

  1. HDU1016 Prime Ring Problem(DFS回溯)

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

  2. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  3. hdu1016 Prime Ring Problem(DFS)

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

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

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

  5. Prime Ring Problem素数环(HDU1016)

    Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...

  6. UVA524 素数环 Prime Ring Problem

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

  7. uva 524 prime ring problem——yhx

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

  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. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

  10. 杭电oj 1016 Prime Ring Problem

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

随机推荐

  1. 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(3)

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  2. 很重要的C++的位运算bitset

    本文摘录于柳神笔记: bitset ⽤来处理⼆进制位⾮常⽅便.头⽂件是 #include , bitset 可能在PAT.蓝桥OJ中不常 ⽤,但是在LeetCode OJ中经常⽤到-⽽且知道 bits ...

  3. @ResponseBody是如何起作用的

    前言 最近参与的项目中,接口中返回的日期格式不对,发现项目中配置了fastjson作为spring的数据转换器,于是使用了fastjson的字段格式化转换注解 发现不起作用.这让我很疑惑,然后在fas ...

  4. WPS/office使用技巧系列

    一 WPS中如果要对比2个文档,不想以标签形式打开而是以多窗口形式,该怎么操作? 点击WPS左上角的蓝色或绿色框-->选项->->视图->勾选在任务栏中显示所有窗口,恢复书签模 ...

  5. apache、mysql、php核心、phpmyadmin的安装及相互关联

    1.apache的安装 https://blog.csdn.net/ashendove/article/details/52206198 里面的serverName  就是你在服务中 设置的apach ...

  6. rem在三星s5部分机型情况下 设置的字体大小与手机实际字体大小不一致问题

    rem在三星s5部分机型情况下 设置的字体大小与手机实际字体大小不一致问题 判断是特殊机型,做特殊处理. var u=navigator.userAgent; if($(window).width() ...

  7. [运维] 如何在云服务器上安装 MySQL 数据库, 并使用 Navicat 实现远程连接管理

    .•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。.:*.•●•✿.。. ...

  8. springcloud-zuul初级篇

    一 前言 zuul路由网关的核心作用是用于后台服务的统一管理:由于微服务是部署在多台服务器上,服务器的ip地址并不能统一,我们需要暴露一个统一的ip地址给前台使用进行接口调用:zuul就是起到路由网关 ...

  9. 「SDOI2009」Bill的挑战

    「SDOI2009」Bill的挑战 传送门 状压 \(\text{DP}\) 瞄一眼数据范围 \(N\le15\),考虑状压. 设 \(f[i][j]\) 表示在所有串中匹配到第 \(i\) 位字符且 ...

  10. vb.net与vb的区别

    本文链接:https://blog.csdn.net/dfshsdr/article/details/63255645最近接触了vb.net,它增加了vb的很多特性,而且演化成为完全面向对象的编程语言 ...