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. UVa 1343 The Rotation Game (状态空间搜索 && IDA*)

    题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使 ...

  2. 按键——Button事件监听器

    button_1.setOnClickListener(new View.OnClickListener() {               @Override    public void onCl ...

  3. Spring Cloud Stream教程(二)主要概念

    Spring Cloud Stream提供了一些简化了消息驱动的微服务应用程序编写的抽象和原语.本节概述了以下内容: Spring Cloud Stream的应用模型 Binder抽象 持续的发布 - ...

  4. wannafly 练习赛11 E 求最值(平面最近点对)

    链接:https://www.nowcoder.com/acm/contest/59/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536K 64bit ...

  5. 京东面试题:Java中 ++i 的操作是线程安全的么?为什么?如何使其线程安全呢?

    你真的了解volatile关键字吗?http://blog.csdn.net/FansUnion/article/details/79495080 面试题:为什么最后两行没有运行?http://blo ...

  6. 6.并发编程--volatile

    并发编程--volatile volatile-说明 volatile关键字的作用是变量在多个线程可见: volatile 关键字是非原子性的 要是实现原子性操作,建议使用atomic类的系列对象:支 ...

  7. hibernate中的@GeneratedValue与@GenericGenerator

    1.GeneratedValue与GenericGenerator的区别 @GeneratorValue注解----JPA通用策略生成器 @GenericGenerator注解----自定义主键生成策 ...

  8. AJAX向Django后端提交POST请求

    一.ajax登录示例 二.CSRF跨站请求伪造 方式一 方式二 方式三 方式四 一.ajax登录示例 urls.py from django.conf.urls import url from dja ...

  9. windows程序调试

    由于不能在控制台输出,可以使用Messagebox 但是有时候要用到输出int之类的,需要转换.转换过程中有会有很多问题. 这里给出两个可行的代码 int a = 5, b = 10; int res ...

  10. python3 导入模块

    python3导入模块和python2  有些不同   需要指定相对目录 如,在Project下有一个nlp目录里面有一个ltp模块,则 from n1.ltp import Clawer