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 思路:DFS遍历所有结果
AC Code:
#include<iostream>
#include<cstring>
using namespace std;
int num[];
int vis[];
int n;
bool isPrime (int k) {
for (int i = ; i * i <= k; i++)
if (k % i == )
return false;
return true;
}
void dfs (int k) {
if (k > n) {
if (isPrime (num[] + num[n])) {
for (int i = ; i < n; i++)
printf("%d ", num[i]);
printf("%d\n", num[n]);
}
return ;
}
for (int i = ; i <= n; i++) {
if (vis[i] == false) {
if (isPrime (num[k - ] + i)) {
num[k] = i;
vis[i] = true;
dfs (k + );
vis[i] = false;
}
}
}
return ;
}
int main() {
int sum = ;
while(~scanf ("%d", &n)){
printf ("Case %d:\n", sum++);
memset (vis, false, sizeof(vis));
num[] = ;
vis[] = true;
dfs();
putchar('\n');
}
}

Prime Ring Problem HDU - 1016的更多相关文章

  1. Prime Ring Problem HDU - 1016 (dfs)

    Prime Ring Problem HDU - 1016 A ring is compose of n circles as shown in diagram. Put natural number ...

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

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

  3. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

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

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

  5. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  6. hdu 1016 Prime Ring Problem(DFS)

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

  7. hdu 1016 Prime Ring Problem(深度优先搜索)

    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. HDU 1016 Prime Ring Problem (回溯法)

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

随机推荐

  1. linux内核中侧async_tx是什么?

    答: 异步内存传输api(asynchronous memory transfer/transform API),这是一种api,用来为应用提供操作DMA的API. 下图是async_tx在架构中所处 ...

  2. 最小二乘法拟合非线性函数及其Matlab/Excel 实现

    1.最小二乘原理 Matlab直接实现最小二乘法的示例: close x = 1:1:100; a = -1.5; b = -10; y = a*log(x)+b; yrand = y + 0.5*r ...

  3. p3302 [SDOI2013]森林(树上主席树+启发式合并)

    对着题目yy了一天加上看了一中午题解,终于搞明白了我太弱了 连边就是合并线段树,把小的集合合并到大的上,可以保证规模至少增加一半,复杂度可以是\(O(logn)\) 合并的时候暴力dfs修改倍增数组和 ...

  4. 论文笔记:Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments

    Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments 2017-10-25  16:38:23   [Proj ...

  5. [easyui] - 在easyui的table中展示提示框

    因为在easyui的table中字段过多,而无法展示全时,被迫只能使用这个方法. 使用方式: 在 $('#dg').datagrid({ 后的 queryParams: form2Json('sear ...

  6. Twitter开发2

    There are different API families The standard (free) Twitter APIs consist of REST APIs and Streaming ...

  7. Druid介绍

    Druid (大数据实时统计分析数据存储) Druid 是一个为在大数据集之上做实时统计分析而设计的开源数据存储.这个系统集合了一个面向列存储的层,一个分布式.shared-nothing的架构,和一 ...

  8. HDU 5976 Detachment(拆分)

    HDU 5976 Detachment(拆分) 00 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem D ...

  9. Gym 100247B Similar Strings(哈希+思维)

    https://vjudge.net/problem/Gym-100247B 题意: 如果两个字符串通过映射后是一样的,则说明这两个字符串是相似的,现在给出n个字符串,计算出有多少组字符串是相似的. ...

  10. 为DataGridView增加鼠标滚轮功能

    #region 鼠标滚动 [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "Wi ...