题意:输入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. JS - 处理浏览器兼容之 event

    function test(e){ var event = e || windows.event   //  IE : windows.event  ,非IE : e }

  2. C/C++ - 多线程

    前几天简单对C和C++中的创建多线程的函数进行了测试,这篇随笔就简单介绍一下创建线程的相关函数. C中三个创建线程函数:pthread_create()._beginthread().CreateTh ...

  3. IDEA 打 jar包

    方法一:Maven插件打包(我用此方法解决的问题) 报错:找不到主类Main class,找不到某个依赖的 jar包 解决方法:将未找到的依赖 jar,使用mvn命令打包放入到我们的本地mvn仓库,I ...

  4. 利用kali自带的msfvenom工具生成远程控制软件(木马)

    2.生成一个简单的木马 3. 4. 5. 6.接下来生成的winx64muma.exe实际演示 7.将生成的winx64muma.exe在受害者机器上运行 8.在kali下输入msfconsole 9 ...

  5. 题解 CF165D 【Beard Graph】

    思路:将黑边标记为1,白边标记为100000,树链剖分 如果查询时ans超过100000,那就有白边,输出-1,不然直接输出ans #include<bits/stdc++.h> #def ...

  6. 《Java周边》IDEA 创建Gradle子项目

    1. 创建Gradle项目 新建项目选择左侧gradle,右侧选择自己jdk版本,勾选java项目,点击Next 写上GroupId组名ArtifactId项目名后点击Next 这里选择使用本地gra ...

  7. ORACLE 删除重复的数据

    内容转自:https://www.cnblogs.com/zfox2017/p/7676237.html         查询及删除重复记录的SQL语句   1.查找表中多余的重复记录,重复记录是根据 ...

  8. spring mvc ,spring boot 整合swagger

    https://blog.csdn.net/qq_35992900/article/details/81274436

  9. CH10 泛型算法

    概述 大多数算法都定义在algorithm头文件中. Note:算法永远不会执行容器操作 泛型算法本身不会执行容器的操作,而是通过迭代器来访问.修改等操作 10.1 题目要求读取数据存入vector, ...

  10. 回文数索引(string类erase解题)

    题目描述 给定一个仅由小写字母组成的字符串.现在请找出一个位置,删掉那个字母之后,字符串变成回文.请放心总会有一个合法的解.如果给定的字符串已经是一个回文串,那么输出-1. 输入描述: 第一行包含T, ...