Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 66391    Accepted Submission(s): 28456

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
 
//HD1016
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
static int j = ;
int n, a[];
bool prime[], vis[]; bool is_prime(int m)//素数判断
{
if (m == )//1不是素数
return false;
for (int i = ; i*i <= m; i++)//素数只能被1和本身整除
if (m % i == )
return false;
return true;
}
void dfs(int cur)
{
if (cur == n && prime[a[] + a[n - ]])//递归出口,当到最后一个数并且首位相加为素数就结束递归
{
for (int i = ; i < n; i++)
{
cout << a[i];
if (i < n - )
cout << ' ';
else
cout << endl;
}
}
else
for (int i = ; i <= n; i++)
if (!vis[i] && prime[i + a[cur - ]])
{
a[cur] = i;
vis[i] = ;
dfs(cur + );
vis[i] = ;
}
} int main()
{
memset(a, , sizeof(a));
memset(prime, false, sizeof(prime));
memset(vis, false, sizeof(vis));
for (int i = ; i < ; i++)
prime[i] = is_prime(i);
while (scanf("%d", &n) != EOF)
{
a[] = ;
printf("Case %d:\n", j++);
dfs();
cout << endl;
}
return ;
}

hdu1016 Prime Ring Problem(DFS)的更多相关文章

  1. 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 ...

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

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

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

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

  6. Prime Ring Problem(dfs水)

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

  7. Prime Ring Problem (DFS练习题)

    K - Prime Ring Problem ============================================================================= ...

  8. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  9. hdu1016 Prime Ring Problem

    dfs,用全局数组和变量保存并更新当前状态. 答案可以直接在搜索结束时打印,n为奇数时方案数为0. acm.hdu.edu.cn/showproblem.php?pid=1016 #include & ...

随机推荐

  1. 使用HttpClient进行Post通信

    ---------------siwuxie095                         首先到 Apache官网 下载相关的库文件     Apache官网:http://www.apac ...

  2. 使用HttpClient进行Get通信

    --------------siwuxie095                             首先到 Apache官网 下载相关的库文件     Apache官网:http://www.a ...

  3. win10 requireAdministrator设置开机自启动无效的解决方案

    开发了一个wpf程序,需要管理员权限,设置了requireAdministrator 同时需要开机自启动,所以添加了注册表: using (RegistryKey key = Registry.Cur ...

  4. TinkerPop中的遍历:图的遍历步骤(3/3)

    48 Project Step project() 步骤(map)将当前对象投射到由提供的标签键入的Map<String,Object>中. gremlin> g.V().out(' ...

  5. redis系列:集群

    1 简介 Redis 集群是Redis 的一个分布式实现,它是一个网状结构,每个节点都通过 TCP 连接跟其他每个节点连接.现在来看看Redis集群实现了哪些目标? 在1000个节点的时候仍能表现得很 ...

  6. win7 64位 [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认

    问题描述: VBA程序连接Access数据库,Excel中执行相关宏,提示[Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认 解决方案: 1.重新安装 AccessDa ...

  7. [raspberry pi3] raspberry 充当time machine

    之前是用硬盘直接当timemachine的,看到有人用raspberry+硬盘充当timemachine的 自己的也搞了下,还是蛮方便的,下面是具体的步骤 1.安装必要的服务 sudo apt-get ...

  8. SQL Server 2014 清理日志

    USE [master] GO ALTER DATABASE [TempTestDb02] SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE [Te ...

  9. 【IIS】编译器失败,错误代码为 -2146232576

    把新写的 ASP.NET MVC 代码发布到 阿里云虚拟机后,报如下错误: 编译器失败,错误代码为 -2146232576 参考了 iis 错误 -2146232576 和 asp.net mvc5本 ...

  10. WebAPI 请求跨域问题

    本人采用的是利用CORS解决跨越问题. 首先利用Nuget 安装“microsoft.aspnet.webapi.cors”,如下图所示: 紧接着,在WebApiConfig文件中加入 config. ...