Hdu 1016 Prime Ring Problem (素数环经典dfs)

时间:2021-07-23 08:25:44

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.

Hdu 1016 Prime Ring Problem (素数环经典dfs)

 
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&lpar;HDU&rpar;&period;1016 Prime Ring Problem &lpar;DFS&rpar;

    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. &lbrack;HDU 1016&rsqb;--Prime Ring Problem&lpar;回溯&rpar;

    题目链接: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&plus;回溯)

    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. T-SQL 拆分使用指定分隔符的字符串&lpar;split string&rpar;

    比如有一个表,我们需要些一个语句像SELECT OtherID, SplitData WHERE SomeID = 'abcdef-.......' , 然后就能返回分割成单独的行. 原表: | So ...

  2. 一步一步安装UEFI分区方式的windows 10 企业版

    发现很多坛友不会安装UEFI分区的windows 10 从启动设置,到分区,到最后的引导与激活都是很大的问题. 在我看来这是最不容易出错的安装方式适合于刚刚上手的菜鸟,自己按照图片一步一步的就可以安装 ...

  3. &lbrack;BZOJ 3218&rsqb;a &plus; b Problem

    又是一道主席树优化网络流的好题 按约大爷的教导,源点为白,汇点为黑,搞成最小割 发现暴力连边要爆炸,但是要连的点在线段树中都构成了一个区间,果断主席树优化之 为什么不用一般线段树? 因为要满足 j&l ...

  4. Android四大组件之Activity详解——创建和启动Activity

    前面我们已经对Activity有过简单的介绍: Android开发——初始Activity Android开发——响应用户事件 Android开发——Activity生命周期 先来看一下最终结果 项目 ...

  5. &lbrack;整理&rsqb;VS2013常用插件

    VS2013常用插件 (工欲善其事,必先利其器.VS2013全攻略(技巧,快捷键,插件)[http://developer.51cto.com/art/201404/437282_all.htm] 代 ...

  6. &lbrack;转&rsqb;没有了SA密码,无法Windows集成身份登录,DBA怎么办?

    没有了SA密码,无法Windows集成身份登录,DBA怎么办?  原文:http://www.cnblogs.com/i6first/p/3512779.html 一同事反馈SQL无法正常登录了,以前 ...

  7. Android listview中使用checkbox

    最近比较忙碌,我也不知道忙的什么东西,打算写的博客写了一半,还没写完,今天先扯一扯项目中遇到的一个问题,一方面防止以后遇到这个问题忘记如何解决,另一方面希望可以提供给遇到同样问题的朋友一个思路.下面开 ...

  8. JAVA对象之生

    https://yq.aliyun.com/users/1556056716932876?spm=5176.100239.blogrightarea55811.3.6cvyJd

  9. 每天学习一点点&period;&period;&period;css&period;&period;&period;

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  10. python基础6 迭代器 生成器

    可迭代的:内部含有__iter__方法的数据类型叫可迭代的,也叫迭代对象实现了迭代协议的对象 运用dir()方法来测试一个数据类型是不是可迭代的的. 迭代器协议是指:对象需要提供next方法,它要么返 ...