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. 前x个数据中至少有m个元素最小值与最大值之差不超过K

    题意 给一组数据,从左到右开始,寻找最小的x,使得第1个元素到第x个元素中,至少存在m个数据,最小值与最大值之差不超过K. INPUT 第一行是T,代表数据组数 每组数据的第一行是三个整数,n.m.k ...

  2. Qt实现通用模板单例

    #include <QAtomicPointer> #include <QMutex> #include <memory> using namespace std; ...

  3. Excel实用知识1

    纯手打,可能有错别字,使用的版本是office2013 转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6329509.html ,谢谢 使用现成的模板 ”开头的 ...

  4. linux mysql 查看数据库大小

    SELECT CONCAT(TRUNCATE(SUM(data_length)//,),'MB') AS data_size, CONCAT(TRUNCATE(SUM(max_data_length) ...

  5. 操作系统OS - 反置页表

    1. https://blog.csdn.net/wuyuegb2312/article/details/16359821 2. https://www.youtube.com/watch?v=YQ3 ...

  6. 【代码总结】GD库中添加图片水印

    函数 getimagesize() bool imagecopymerge( resource dst_im, resource src_im, int dst_x, int dst_y, int s ...

  7. day1-2基本数据类型

    /*5种基本数据类型 1,Undefined var a; a = undefined Undefined派生自Null 2,Null(本质属于object,单独细分出来的) var a = null ...

  8. linux 删除 复制 移动

    Linux文件类型 - 普通文件 d 目录文件 b 块设备 c 字符设备 l 符号链接文件 p 管道文件pipe s 套接字文件socket 基名:basename 目录名:dirname basen ...

  9. JQuery 实现PPT效果,点跳目标页及翻页(待改进)

    实现PPT效果 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...

  10. Android编程实现点击链接打开APP功能示例

    本文实例讲述了Android编程实现点击链接打开APP功能.分享给大家供大家参考,具体如下: 在Android中点击链接打开APP是一个很常见的需求.例如,电商为用户发送优惠券之后经常会下发一条短信: ...