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. Python标准库:内置函数format(value[, format_spec])

    的值的函数value按format_spec的格式来格式化,然而函数解释format_spec是依据value的类型来决定的.不同的类型有不同的格式化解释. 当參数format_spec为空时,本函数 ...

  2. 金蝶K3无法创建数据库,请查看该文件夹的错误的解决方法。

    无法创建数据库! 检查你的文件夹C:\XXX\DATA是否存在.并且该系统是不低,或SQL Server服务的启动用户不具备<K3ERP\DBFILE>文件夹的写权限.请改动Windows ...

  3. 【Cocos得知】技术要点通常的积累

    1.粒子特效 CCParticleSystem*sp = CCParticleSnow::create(); sp->setTexture(CCTextureCache::sharedTextu ...

  4. jsp页面onsubmit=&quot;return checklogin();&quot;该解决方案给

    选择Window->Preferences->MyEclipse->Validation 去掉方框里的对号,然后Apply 然后点击Yes->然后再点击ok->Yes,足 ...

  5. js之with

    网上找资料的时候看到有js代码里出现了大量的with语句,有点好奇,这里做下总结: 1)简要说明         with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性. ...

  6. 经典算法题每日演练——第八题 AC自动机

    原文:经典算法题每日演练--第八题 AC自动机 上一篇我们说了单模式匹配算法KMP,现在我们有需求了,我要检查一篇文章中是否有某些敏感词,这其实就是多模式匹配的问题. 当然你也可以用KMP算法求出,那 ...

  7. crawler_jsoup HTML解析器_使用选择器语法来查找元素

    参照:http://www.open-open.com/jsoup/selector-syntax.htm 使用选择器语法来查找元素 问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. ...

  8. 在面对变化,撇开NO

    参观后转到供应商,看到自己的生产线流水线半自己的钣金生产线举措.这就是我一直想厂提高生产现场的想法,因为通常当我看到工作人员努力工作和繁忙的生产,只见废现场,线解决方式时,有点莫名的兴奋. 幸亏是一家 ...

  9. 【百度地图API】如何调整结果面板的样式?如何获取指定页码的结果?

    原文:[百度地图API]如何调整结果面板的样式?如何获取指定页码的结果? 摘要: 1.你是否想自定义查询后,结果面板的显示样式? 2.数据接口每次只返回10条结果,如何取到单独每一页的结果? ---- ...

  10. STL 源代码分析 算法 stl_algo.h -- binary_search

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie binary_search -------------------------------- ...