题意:输入n,把1~n组成个环,相邻两个数之和为素数。

分析:回溯法。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int prime[MAXN];
int a[MAXN];
int vis[MAXN];
int n;
void init(){
for(int i = ; i < MAXN; ++i) prime[i] = ;
for(int i = ; i < MAXN; ++i){
if(prime[i]){
for(int j = i + i; j < MAXN; j += i){
prime[j] = ;
}
}
}
}
void dfs(int cur){
if(cur == n && prime[a[n] + a[]]){
for(int i = ; i <= n; ++i){
if(i != ) printf(" ");
printf("%d", a[i]);
}
printf("\n");
}
else{
for(int i = ; i <= n; ++i){
if(!vis[i] && prime[a[cur] + i]){
vis[i] = ;
a[cur + ] = i;
dfs(cur + );
vis[i] = ;
}
}
}
}
int main(){
init();
a[] = ;
int kase = ;
while(scanf("%d", &n) == ){
if(kase) printf("\n");
printf("Case %d:\n", ++kase);
dfs();
}
return ;
}

UVA - 524 Prime Ring Problem(素数环)(回溯法)的更多相关文章

  1. UVA - 524 Prime Ring Problem(dfs回溯法)

    UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & % ...

  2. UVa 524 Prime Ring Problem(DFS , 回溯)

    题意  把1到n这n个数以1为首位围成一圈  输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的  n最大为16  利用回溯法 边生成边推断  就要快非常多了 #inc ...

  3. Hdu 1016 Prime Ring Problem (素数环经典dfs)

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

  4. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  5. 题目1459:Prime ring problem(素数环问题——递归算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  6. UVa 524 Prime Ring Problem(回溯法)

    传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...

  7. UVa 524 Prime Ring Problem【回溯】

    题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...

  8. HDOJ 1016 Prime Ring Problem素数环【深搜】

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

  9. UVa 524 - Prime Ring Problem

    题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...

随机推荐

  1. 第2节 Scala中面向对象编程:9、getClass和classOf;10、调用父类的constructor;11、抽象类和抽象字段;

    6.3.4.     Scala中getClass 和 classOf Class A extends class B B b=new A    b.getClass ==classOf[A] B b ...

  2. 前后端分离后API交互如何保证数据安全性?

    一.前言 前后端分离的开发方式,我们以接口为标准来进行推动,定义好接口,各自开发自己的功能,最后进行联调整合.无论是开发原生的APP还是webapp还是PC端的软件,只要是前后端分离的模式,就避免不了 ...

  3. IntelliJ IDEA 破解之后,用了一段时间后,打开软件提示 no suitable licenses left on the license server

    IntelliJ IDEA 破解之后,用了一段时间后,打开软件提示 no suitable licenses left on the license server 需要让我们重新注册,原来是之前的地址 ...

  4. SSM项目中,关于Test类中不能使用Autowired注入bean的问题

    在测试类中使用AutoWired注解一直不能获取到Bean,调用方法时一直报空指针异常,我有在其他类中使用AutoWired试了下,发现能够生效.问题应该就是处在Test类中,后面找了半天终于找到问题 ...

  5. maven缺失ojdbc6解决方案 :Missing artifact com.oracle:ojdbc6:jar:11.2.0.1.0问题解决 ojdbc包pom.xml出错

    问题已解决,感谢博主,给您磕头了. | | 解决方法就是把缺少的 jar 手动添加到本地仓库中,再重新引入依赖即可.详情请参考以下链接. | | 转发自: https://blog.csdn.net/ ...

  6. 142、Java内部类之在普通方法里面定义内部类

    01.代码如下: package TIANPAN; class Outer { // 外部类 private String msg = "Hello World !"; publi ...

  7. JavaScript内置对象Array、String 的方法

    Array push() //将一个或多个数据加入到数组的末端,并返回新的数组长度. pop() //取出数组中的最后一项,修改length属性,并返回被删除的数据 shift() //取出数组中的第 ...

  8. Linux CentOS7 VMware 相对和绝对路径、cd命令、mkdir/rmdir、rm命令——笔记

    一. 相对和绝对路径 绝对路径是从/(也被称为根目录)开始的,比如/usr.cd /root/ pwd 注:判断用户当前所处的位置 相对路径是以 . 或 .. 开始的 二.cd命令 cd 是进入到当前 ...

  9. 解题报告+板子:luogu P3387 【模板】缩点

    题目链接:P3387 [模板]缩点 缩点板子,所谓\(dp\)就是拓扑排序(毕竟可以重走边),像\(SPFA\)一样松弛就好,就是重边极其烦人,还加了排序(绝对自己想的,然鹅拓扑的思路不是). 下面上 ...

  10. CH11 关联容器

    关联容器与顺序容器有着根本的不同:关联容器中的元素是按关键字来保存和访问的,而顺序容器是按它们在容器中的位置来顺序保存和访问的.两个主要的关联容器:map和set map 中的元素的是一个key-va ...