题意:输入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. android 简单列表对话框(AlertDialog.Builder().setItems())

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  2. centos7中redis安装

    官网地址:http://redis.io/ 官网下载地址:http://redis.io/download 1. 下载Redis源码(tar.gz),并上传到Linux:或 wget http://d ...

  3. 搜索栏UISearchBar的使用

    本文结构: 1.首先是对UISearchBar的简介文字 2.初始化展现UISearchBar,并解析它的结构 3.属性.方法.代理等的一一介绍 4.日常的使用,包括单独对UISearchBar的配置 ...

  4. android使用友盟实现第三方登录、分享以及微信回调无反应问题解决办法

    这里介绍微信和新浪登录.微信登录和新浪登录都需要申请第三方账号.可以参考官方文档http://dev.umeng.com/social/android/operation#2还是很清晰的. 新浪微博开 ...

  5. Day3-E-New Year Snowmen CodeForces140C

    As meticulous Gerald sets the table and caring Alexander sends the postcards, Sergey makes snowmen. ...

  6. 086、Java数组之对象数组的动态初始化

    01.代码如下: package TIANPAN; class Book { private String title; private double price; public Book(Strin ...

  7. Unity 公告板 Billboard

    创建脚本如下 Billboard.cs using UnityEngine; using System.Collections; public class Billboard : MonoBehavi ...

  8. 1-3SpringBoot之Controller使用

    Controller的使用 @Controller 处理http请求 @RestController Spring4之后新加的注解,原来返回json需要@ResponseBody配合@Controll ...

  9. UGUI崭新崭新的新手

    ------------------------------------------------------------------------------------1--------------- ...

  10. Vue + Webpack 根据不同环境打包

    修改 prod.env.js // 当前正在运行的脚本名称 const TARGET = process.env.npm_lifecycle_event // 第一个参数 let argv = pro ...