/*
将1-n个数放在环中,保证相邻的两个数的和是素数
第一个数字永远是1
就这两个约束条件
第一个难点是计算素数;
参考文献:
http://c.biancheng.net/cpp/html/2547.html
http://blog.csdn.net/liukehua123/article/details/5482854 第二个部分是深度递归;
http://blog.csdn.net/a402630999/article/details/7208218
http://www.cnblogs.com/jiangjing/archive/2013/02/04/2891621.html
*/ #include <stdio.h>
#include <stdlib.h>
int n;
int book[],a[];
int isPrime[]; int get_prime()
{
for(int i=;i<;i++)
{
if(!isPrime[i])
{
for(int j=i*i;j<;j+=i)
isPrime[j]=;
}
}
return ;
} int dfs(int x)
{
if(x==n&&!isPrime[a[]+a[n-]])//最后一个已经放进去,并且最后一对首尾也满足条件
{
for(int i=;i<x-;i++)
printf("%d ",a[i]);
printf("%d\n",a[x-]);
return ; }
for(int i=;i<=n;i++)
{
if(book[i]==&&!isPrime[a[x-]+i])//没被使用,且上一个放进去的数和当前打算放进去的数的和是素数
{
a[x]=i;
book[i]=;
dfs(x+);
book[i]=;
}
}
return ;
}
int main()
{
//freopen("input.txt","r",stdin);
int num=;
while(scanf("%d",&n)!=EOF)
{
num++;
a[]=;
get_prime();
for(int i=;i<;i++)
book[i]=;
printf("Case %d:\n",num);
dfs();
printf("\n");
}
return ;
}

HDU-1016-素数环的更多相关文章

  1. HDU 1016 素数环(dfs + 回溯)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 一道很典型的dfs+回溯: 根据题意首先进行初始化,即第一个位置为1,然后进行dfs, ...

  2. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

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

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

  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. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

  7. hdu 1016

    这是一道考搜索的题目.这道题我用深搜解决了,不过说实话自己对于深搜理解得并不深刻,在这里对于这一题总结一下. 这道题输入为一个实数n,要求输出有1~n这n个数所组成的所有素数环(这是素数环),素数环的 ...

  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. nyoj 488 素数环

    素数环 时间限制:1000 ms  |  内存限制:65535 KB 难度:2   描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简 ...

  10. nyoj 素数环

    算法:搜索 描述 有一个整数n,把从1到n的数字无重复的排列成环,且使每相邻两个数(包括首尾)的和都为素数,称为素数环. 为了简便起见,我们规定每个素数环都从1开始.例如,下图就是6的一个素数环. 输 ...

随机推荐

  1. <转载>构造函数与拷贝构造函数

    原文地址http://www.cnblogs.com/waynelu/archive/2012/07/01/2572264.html 构造函数 构造函数.析构函数与赋值函数是每个类最基本的函数. 对于 ...

  2. jquery.validate详解一

    jQuery校验 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一导入js库 <script src=&q ...

  3. 如何在已经存在python2的linux环境上安装python3

    最近看到好多人都在问在已经存在python2.7的环境下如何安装python3,于是我决定写下这篇文档,供大家学习参考,希望能够给大家带来帮助 有的人在安装的时候可能会先将python2卸载掉,这个地 ...

  4. LeetCode——Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. JMeter一个错误the target server failed to respond--JMeter坑

    问题:1.在测试一个http景象,特别是集波动TPS时刻,出现了一个错误.它现在是一个必须错误(压力顺利时却零星的错误,甚至很少见): 每次必现错误(開始一直怀疑是网络或程序的问题)   2.失败事务 ...

  6. Android日志系统驱动程序Logger源代码分析

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6595744 我们知道,在Android系统中, ...

  7. 初始——第一款个人开发上线app store

    最初学习iOS开发时就听人建议,程序员应该有自己的博客,来记录每天的收获,于人于己都是一件很有意义的事.但当初作为菜鸟一枚,自认为对一些知识的认识尚浅,写博客这种高大上的事和自己八竿子打不着. 现如今 ...

  8. Content-Disposition的使用和注意事项(转载)

    Content-Disposition的使用和注意事项 最近不少Web技术圈内的朋友在讨论协议方面的事情,有的说web开发者应该熟悉web相 关的协议,有的则说不用很了解.个人认为这要分层次来看待这个 ...

  9. hdu1020Encoding

    Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...

  10. (转) class II

    Overloading operators   Classes, essentially, define new types to be used in C++ code. And types in ...