搜索专题: HDU1016Prime Ring Problem
Prime Ring Problem
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52146 Accepted Submission(s): 23096
Note: the number of first circle should always be 1.
lexicographical order.
You are to write a program that completes above process.
Print a blank line after each case.
6
8
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4 Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2
Asia 1996, Shanghai (Mainland China)
RunId : 21243288 Language : G++ Author : hnustwanghe
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 20 + 5;
bool prime[N*3],used[N]={0};
int a[N],n;
void prime_factor(){
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i=2;i<=N*2;i++)
if(!prime[i]){
for(int j=i*i;j<=N;j+=i)
prime[j] = 1;
}
}
void DFS(int cur){
if(cur > n){
if(!prime[a[1]+a[n]])
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
return;
}
for(int i=2;i<=n;i++){
if(!used[i]){
if(!prime[a[cur-1]+i]){
used[i] = true;
a[cur] = i;
DFS(cur+1);
used[i] = false;
}
}
}
}
int main(){
a[1] = 1;
prime_factor();
int cnt = 0;
while(scanf("%d",&n)==1){
printf("Case %d:\n",++cnt);
DFS(2);
printf("\n");
}
}
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N = 20 + 5;
bool prime[N*3],used[N]={0};
int a[N],n;
void prime_factor(){
memset(prime,0,sizeof(prime));
prime[0] = prime[1] = 1;
for(int i=2;i<=N*2;i++)
if(!prime[i]){
for(int j=i*i;j<=N;j+=i)
prime[j] = 1;
}
} void DFS(int cur){
if(cur > n){
if(!prime[a[1]+a[n]])
for(int i=1;i<=n;i++)
printf("%d%c",a[i],i==n?'\n':' ');
return;
}
for(int i=2;i<=n;i++){
if(!used[i]){
if(!prime[a[cur-1]+i]){
used[i] = true;
a[cur] = i;
DFS(cur+1);
used[i] = false;
}
}
}
} int main(){
a[1] = 1;
prime_factor();
int cnt = 0;
while(scanf("%d",&n)==1){
printf("Case %d:\n",++cnt);
DFS(2);
printf("\n");
}
}
搜索专题: HDU1016Prime Ring Problem的更多相关文章
- 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem
/* Name: NYOJ--488--素数环 Author: shen_渊 Date: 15/04/17 15:30 Description: DFS,素数打个表,37以内就够用了 */ #incl ...
- DFS——hdu1016Prime Ring Problem
一.题目回顾 题目链接:Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagra ...
- hdu1016Prime Ring Problem
就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...
- hdu 1016 Prime Ring Problem(深度优先搜索)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- UVA524-Prime Ring Problem(搜索剪枝)
Problem UVA524-Prime Ring Problem Accept:6782 Submit:43814 Time Limit: 3000 mSec Problem Descripti ...
- hdu 1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 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(dfs回溯法)
UVA - 524 Prime Ring Problem Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & % ...
- HDU 1016 Prime Ring Problem (回溯法)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- 2019学军集训记&PKUWC2020游记
题解:https://www.cnblogs.com/gmh77/p/12051260.html 集训(×) 被虐(√) Day1 二段考 Day2 绝对不鸽 没那回事 还在路上 其实就是咕了两天 晚 ...
- C/C++运算符优先级关系
C/C++优先级 从高到低 1~14梯队 1. () [] . -> 2. ! ~ -(负号) ++ -- &(取变量地址)* (type)(强制类型) ...
- 创建一个Django项目
创建一个django项目: 1. django-admin startproject student_manage(项目名) 2. cd student_manage python manage.p ...
- oracle 7.4安装nvidia驱动
2019-8-28 参考网页: 如何在k8s集群中安装nvidia.cuda并使用GPU进行训练 https://blog.csdn.net/u013042928/article/details/78 ...
- 解决html5大文件断点续传
一.概述 所谓断点续传,其实只是指下载,也就是要从文件已经下载的地方开始继续下载.在以前版本的HTTP协议是不支持断点的,HTTP/1.1开始就支持了.一般断点下载时才用到Range和Content- ...
- [BZOJ1718]:[Usaco2006 Jan] Redundant Paths 分离的路径(塔尖)
题目传送门 题目描述 为了从F个草场中的一个走到另一个,贝茜和她的同伴们有时不得不路过一些她们讨厌的可怕的树.奶牛们已经厌倦了被迫走某一条路,所以她们想建一些新路,使每一对草场之间都会至少有两条相互分 ...
- Tomcat配置多域名 Alias
在Tomcat配置多域名,目的是和apache相对应,实现多域名访问. 使用 < Alias></ Alias>,务必注意,使用的是首字母大写. 我刚开配置使用小写,如果hos ...
- enum简单使用
前台传入weightCode :1/2/3/4,获取不同的区间0~10 10~50 50~100 100~999999 0~999999 public void setWeight(){ this.m ...
- yii2.0 curd操作数据写法
一.执行原生sql查询,创建yii\db\Command insert(),update(),delete()直接构建,相应的sql语句 查: 1.查询一条 \Yii::$app-&g ...
- 每日踩坑 2019-08-23 button 元素点击后刷新页面
button标签按钮会提交表单. 解决方案: <button class="btn btn-primary" type="button" id=" ...