Prime Ring Problem

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20105    Accepted Submission(s): 9001

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
 
Recommend

JGShining

题意:素数环 (经典搜索)

代码:

写法1:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int yes[22];
bool in[22];
bool prim[50];
int n; void primtable()
{
int i,j;
memset(prim,0,sizeof(prim));
for(i=3;i<=50;i++)
{
for(j=2;j<=i-1;j++)
{
if(i%j==0)
prim[i]=true;
}
}
} void dfs(int pos)
{
int i,u;
if(pos>n)
{
int m=yes[1]+yes[n];
if(prim[m]==false)
{
for(i=1;i<=n;i++)
printf("%d%c",yes[i],i==n?'\n':' ');
//printf("\n"); //这里PE了一次
}
return;
}
for(i=2;i<=n;i++)
{
u=i+yes[pos-1];
if(prim[u]==false&&in[i]==false)
{
yes[pos]=i;
in[i]=true;
dfs(pos+1);
in[i]=false;
}
}
} int main()
{
int cas=1;
primtable();
while(scanf("%d",&n)!=EOF)
{
memset(in,0,sizeof(in));
yes[1]=1;
in[1]=true;
printf("Case %d:\n",cas++);
dfs(2);
printf("\n");
}
return 0;
} // 203MS

8765975

2013-07-30 16:19:31

Accepted

203MS

228K

1125 B

C++

写法2:

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int yes[22];
bool in[22];
bool prim[50];
int n; void no_prim()
{
int tmp,i,j;
for(i=3;i<20;i+=2)
{
if(prim[i]==false)
{
tmp=i<<1;
for(j=i*i;j<50;j+=tmp)
prim[j]=true;
}
}
} void dfs(int pos)
{
int i,u;
if(pos>n)
{
int m=yes[1]+yes[n];
if(prim[m]==false&&m%2||m==2)
{
for(i=1;i<=n;i++)
printf("%d%c",yes[i],i==n?'\n':' ');
//printf("\n");
}
return;
}
for(i=2;i<=n;i++)
{ u=i+yes[pos-1];
if((!prim[u]&&u%2||u==2)&&in[i]==false)
{
yes[pos]=i;
in[i]=true;
dfs(pos+1);
in[i]=false;
}
}
} int main()
{
no_prim();
int cas=1;
while(scanf("%d",&n)!=EOF)
{
memset(in,0,sizeof(in));
yes[1]=1;
in[1]=true;
printf("Case %d:\n",cas++);
dfs(2);
printf("\n");
}
return 0;
} // 234MS

8765965

2013-07-30 16:18:52

Accepted

234MS

228K

1143 B

C++

Hdu 1016 Prime Ring Problem (素数环经典dfs)的更多相关文章

  1. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  2. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  3. HDU 1016 Prime Ring Problem(素数环问题)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

  4. [HDU 1016]--Prime Ring Problem(回溯)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...

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

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

  6. HDU - 1016 Prime Ring Problem 经典素数环

    Prime Ring Problem A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., ...

  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. hdu 1016 Prime Ring Problem(dfs)

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

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

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

随机推荐

  1. 设计模式C++实现_1_Singleton设计模式(简单的实现)

    Singleton设计模式 思路如以下: Single.h #pragma once #include <iostream> #include <string> using n ...

  2. 【甘道夫】官方网站MapReduce代码注释具体实例

    引言 1.本文不描写叙述MapReduce入门知识,这类知识网上非常多.请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop ...

  3. thinkphp学习笔记6—url模式

    原文:thinkphp学习笔记6-url模式 入口文件是应用的唯一入口,因为可以多入口,每个应用可以对应一个入口文件,系统会从rul参数中解析当前请求的模块,控制器,操作.ThinkPHP是区分大小写 ...

  4. Crazy Rows

    Problem You are given an N x N matrix with 0 and 1 values. You can swap any two adjacent rows of the ...

  5. C++语言债券系列之十一——友元函数和拷贝构造函数

    1.好友功能 (1)友元函数类的普通功能外定义. 定义友元函数和相同的正常功能.在类必须声明的正常功能为好友. (2)友元函数不是一个成员函数. 你不能反对打电话.但直接调用:友元函数访问类的公共.p ...

  6. EF中的EntityState几个状态的说明

    之前使用EF,我们都是通过调用SaveChanges方法把增加/修改/删除的数据提交到数据库,但是上下文是如何知道实体对象是增加.修改还是删除呢?答案是通过EntityState枚举来判断的,我们看一 ...

  7. js敏感词过滤

    var filterWord={ words:"", tblRoot:{}, //敏感词文件 file:"sensitiveWords.txt", //载入敏感 ...

  8. 前端学习笔记(zepto或jquery)——对li标签的相关操作(三)

    对li标签的相关操作——八种方式遍历li标签并获取其值 $("ul>li").forEach(function(item,index){ alert(index+" ...

  9. css3简单几步画一个乾坤图

    原文:[原创]css3简单几步画一个乾坤图 效果如上,鼠标移上去会有动画. 代码如下非常简单: <html> <head> <style> .outer{heigh ...

  10. 通用Key-Value存储系统的存储管理策略解析

            Key-Value存储作为NoSQL存储的一种常见方式,提供了比SQL数据库更好的可扩展性和读写性能. 比方当前开源最热门的Memcached和Redis:淘宝的Tair.腾讯的Cme ...