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

Problem Description
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.



Note: the number of first circle should always be 1.




 
Input
n (0 < n < 20).
 
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in
lexicographical order.



You are to write a program that completes above process.



Print a blank line after each case.
 
Sample Input
6
8
 
Sample Output
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
 
Source

Asia 1996, Shanghai (Mainland China)

Problem : 1016 ( Prime Ring Problem )     Judge Status : Accepted

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的更多相关文章

  1. 素数环:NYOJ--488--dfs||hdu-1016-Prime Ring Problem

    /* Name: NYOJ--488--素数环 Author: shen_渊 Date: 15/04/17 15:30 Description: DFS,素数打个表,37以内就够用了 */ #incl ...

  2. DFS——hdu1016Prime Ring Problem

    一.题目回顾 题目链接:Prime Ring Problem Problem Description A ring is compose of n circles as shown in diagra ...

  3. hdu1016Prime Ring Problem

     就是说,给你一个数n, 要你把1到n都连在一起成环. 每一个数不可反复, 且相连的两个数的和要是素数. 把全部情况输出来. 我是用dfs暴力出来的. 首先把素数打表, 然后每次顺时针预測下一个数 ...

  4. hdu 1016 Prime Ring Problem(深度优先搜索)

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

  5. UVA524-Prime Ring Problem(搜索剪枝)

    Problem UVA524-Prime Ring Problem Accept:6782  Submit:43814 Time Limit: 3000 mSec  Problem Descripti ...

  6. hdu 1016 Prime Ring Problem(DFS)

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

  7. HDU 1016 Prime Ring Problem(经典DFS+回溯)

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

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

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

  9. HDU 1016 Prime Ring Problem (回溯法)

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

随机推荐

  1. nmon监控使用

    1.上传nmon_linux_x86_64文件到服务器 2.修改文件权限chmod 775 nmon_linux_x86_64 3.压测时需要执行以下命令监控服务器./nmon_linux_x86_6 ...

  2. 通过web传大文件

    上传文件的jsp中的部分 通过form表单向后端发送请求 <form id="postForm" action="${pageContext.request.con ...

  3. Codeforces 960D Full Binary Tree Queries ( 思维 && 模拟 )

    题意 : 给出一颗无限层的满二叉树,然后每个值为 X (根的 X 等于 1 ) 左孩子的值是 2*X,右孩子的值是 2*X+1 ,现在有两种操作,(1, x,k) 表示将 x 所在层的所有节点整体向右 ...

  4. ASP.NET MVC Ajax下载文件(使用NPOI向现有的excel模板文件里面添加数据)

    View Html.DevExpress().Button(DevExpressButtonHelper.AddButton(ViewBag.Form, "Export", &qu ...

  5. Tensorflow视频教程&Pytorch视频教程

    基于tensorflow做研究和基于pytorch做研究哪个好?哪个更容易复制代码,工业上更易用.Keras和tensorflow.pytorch的关系. Keras:Keras是一个由Python编 ...

  6. python3学习笔记(三):注释和字符串

    一.注释 为了让别人能够更容易理解程序,使用注释是非常有效的,即使是自己回头再看旧代码也是一样. # 打印圆的周长: print(2* pi* r) 在python 中用井号(#)表示注释.井号(#) ...

  7. es之批量提交操作

    1:批量查询操作 1):插入测试数据 PUT /costumer/doc/1{ "name": "zhangsan", "age": 20} ...

  8. 在MyEclipse安装Spket插件,用于jQuery代码提示

    Spket插件下载: https://pan.baidu.com/s/1sjz24NF 解压文件,然后将解压后的文件全部复制到MyEclipse安装目录下的dropins包中,重启MyEclipse. ...

  9. @清晰掉 qsort()

    qsort函数描述: http://www.cnblogs.com/sooner/archive/2012/04/18/2455011.html qsort()函数实现: /*** *qsort.c ...

  10. 二、Python基础

    1.变量名 数字,字母,下划线:aaa1;aa_b1 不能以数字开头:1aa 变量名不能是python内部的关键字 2.getpass import getpass username=raw_inpu ...