UVA - 524 Prime Ring Problem(素数环)(回溯法)
题意:输入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(素数环)(回溯法)的更多相关文章
- UVA - 524 Prime Ring Problem(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- UVa 524 Prime Ring Problem(DFS , 回溯)
题意 把1到n这n个数以1为首位围成一圈 输出全部满足随意相邻两数之和均为素数的全部排列 直接枚举排列看是否符合肯定会超时的 n最大为16 利用回溯法 边生成边推断 就要快非常多了 #inc ...
- Hdu 1016 Prime Ring Problem (素数环经典dfs)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- uva 524 prime ring problem——yhx
Prime Ring Problem A ring is composed of n (even number) circles as shown in diagram. Put natural ...
- 题目1459:Prime ring problem(素数环问题——递归算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1459 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- UVa 524 Prime Ring Problem【回溯】
题意:给出n,把从1到n排成一个环,输出相邻两个数的和为素数的序列 照着紫书敲的, 大概就是这个地方需要注意下,初始化的时候a[0]=1,然后dfs(1),从第1个位置开始搜 #include< ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- UVa 524 - Prime Ring Problem
题目大意:输入正整数n,把整数1,2...,n组成一个环,使得相邻两个整数之和均为素数.输出时从整数1开始逆时针(题目中说的不是很明白??)排列.同一个环应恰好输出一次. 枚举,并在枚举每一个数是进行 ...
随机推荐
- (转)notepad++去重
notepad++ 真是强大,几乎你能想到的处理文本方法都可以用它来实现,因为他有强大的插件团! 例如1:去除重复行 先安装TextFx插件 在菜单TextFX-->TextFX Tools下 ...
- Java 理论与实践: 哈希
有效和正确定义hashCode()和equals() 每个Java对象都有 hashCode() 和 equals() 方法.许多类 Override 这些方法的缺省实施,以在对象实例之间提供更深层次 ...
- CentOS安装后的第一步:配置IP地址
有关于centos7获取IP地址的方法主要有两种,1:动态获取ip:2:设置静态IP地址 在配置网络之前我们先要知道centos的网卡名称是什么,centos7不再使用ifconfig命令,可通过命令 ...
- CentOS6.9安装redis
目录 Centos6.9下的Redis安装和配置(最简易方式) redis客户端登录方式 Centos6.9下的Redis安装和配置(最简易方式) 在服务器上创建一个目录/service,然后下载re ...
- netsh命令获取wifi历史连接密码
首先[win+r]快捷键打开运行,输入cmd.或点击左下角win-运行-cmd 1.netsh wlan show profiles //列出所有的ap名称 2.netsh wlan show pro ...
- WebGL绘制正方体
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CentOS7 安装PHP7的swoole扩展:
一.绪 Swoole简介 PHP异步网络通信引擎 最终编译为so文件作为PHP的扩展 准备工作 Linux环境 PHP7 swoole2.1 redis 源码安装PHP7 源码安装swoole htt ...
- Linux centosVMware df命令、du命令、磁盘分区
一.df命令 df(disk filesystem的简写)用于查看自己挂载磁盘的总容量.使用容量.剩余容量,可以不加任何参数,默认以KB为单位. [root@davery ~]# df文件系统 1K- ...
- 阿里云配置mysql
环境:阿里云ECS服务器,系统为centos7.2 用户:root 参考博客:https://blog.csdn.net/kunzai6/article/details/81938613 师兄的哈哈哈 ...
- Linux磁盘
1.磁盘的接口类型与命名方式 磁盘接口分为SATA.SCSI.SAS.PCI-E.光纤FC通道.常见的设备在Linux中的命名如下: 设备 设备在Linux内的文件名 IDE硬盘 /dev/hd[a- ...