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. transient关键字的理解

    谈到这个transient这个关键字,我们应该会立马想到序列化这个过程:什么是序列化?什么又是反序列化呢?序列化就是将对象转化内成二进制,而反序列化就是就二进制文件转换成对象的过程.一旦变量使用了tr ...

  2. solr-用mmseg4j配置同义词索引和检索(IKanlyzer需要修改源码适应solr接口才能使用同义词功能)

    概念说明:同义词大体的意思是指,当用户输入一个词时,solr会把相关有相同意思的近义词的或同义词的term的语段内容从索引中取出,展示给用户,提高交互的友好性(当然这些同义词的定义是要在配置文件中事先 ...

  3. 主机(windows)与VMware虚拟机(linux)互传文件

    网上有不少教程,比如http://bbs.kafan.cn/thread-451327-1-1.html,我仅记录我平时用的方法. 1.VMware tools:  如果有网,且Linux有桌面的,可 ...

  4. cocos2d中setBlendFunc设置颜色混合方案

    CCSprite有一个ccBlendFunc类型的blendFunc_结构体成员,可以用来设置描绘时的颜色混合方案.ccBlendFunc包含了一个src和一个dst,分别表示源和目标的运算因子. 如 ...

  5. 关于c#数据类型,类型转换,变量,常量,转义符。。。

    先说一下数据类型...数据类型可以分为两大类:基本数据类型和引用类型. 基本数据类型按功能又分为“值类型”,“布尔型”,“字符型”. 引用类型分为“字符串”,“时间日期”.  没图没真相↓面放图. 橙 ...

  6. PCL—点云分割(邻近信息) 低层次点云处理

    博客转载自:http://www.cnblogs.com/ironstark/p/5000147.html 分割给人最直观的影响大概就是邻居和我不一样.比如某条界线这边是中华文明,界线那边是西方文,最 ...

  7. GCD 学习(八)dispatch_semaphore

    dispatch_semaphore 信号量基于计数器的一种多线程同步机制.在多个线程访问共有资源时候,会因为多线程的特性而引发数据出错的问题.     dispatch_queue_t queue ...

  8. 718C Sasha and Array

    传送门 题目 Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be ...

  9. C++ string操作(转载)

    1.string类的构造函数 string(const char * s); //用s字符串初始化 string(const string &s);//用string类的s初始化 string ...

  10. java多线程系列:CountDownLatch

    这篇文章将介绍CountDownLatch这个同步工具类的基本信息以及通过案例来介绍如何使用这个工具. CountDownLatch是java.util.concurrent包下面的一个工具类,可以用 ...