K - Prime Ring Problem

=================================================================================================================================
题目大意是给出 1~n 个数 第一个数必定是 1 ,使得无论那两个相邻的数相加,都是质数(即大于1的自然数中,除了1和它本身以外不再有其他因数);
打印出所有可能,即直接用dfs 遍历所有可能性;
我的代码思路:
1. 数组范围很小 最大的和不超过40  则可以直接预处理这个范围内的数是否为质数。
2.如何快速判断是否为质数 ,快速的方法 :先判断2之后  3~sqrt(n) 之间所有的奇数 是否存在其约数。
3.用tail数组储存列表。
4.一般dfs的套路 使用book 标记是否使用过,开始遍历。
=================================================================================================================================
代码:
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
int n;
bool prime[];
void Prime() //预处理1~40之间的数是否为质数
{
for(int i = ;i <= ;++i)
{
bool flag = ;
for(int j = ;j<=sqrt(i);j==?++j:j+=)
if(i%j==) {flag = ;break;}
flag==?prime[i]=:prime[i]=; //1即为质数0则否
}
}
int tail[]; //列表
void print() //打印列表
{
for(int i =;i<=n;++i)
printf(i==n?"%d\n":"%d ",tail[i]);
}
bool book[]; //标记
void dfs(int x)
{
if(x==n)
{ //最后再判断最后一个数与第一个数相加是否为质数
if(prime[tail[n]+tail[]]) print();
return;
}
for(int i = ;i<=n;++i)
{
if(prime[i+tail[x]]&&book[i]==)//标准dfs套路↓
{
tail[x+] = i;
book[i] = ;
dfs(x+);
book[i] = ;
}
}
}
int main()
{
int cas = ;
Prime();
tail[] = ; book[] = ;
while(~scanf("%d",&n))
{
memset(book,,sizeof(book));
printf("Case %d:\n",++cas);
if(n==) printf("1\n");
else if(n%==) ; //如果是奇数,铁定实现不了
else dfs();
putchar('\n');
}
}

Prime Ring Problem (DFS练习题)的更多相关文章

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

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

  2. HDU 1016 Prime Ring Problem (DFS)

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

  3. Prime Ring Problem(dfs水)

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

  4. Hdu1016 Prime Ring Problem(DFS) 2016-05-06 14:27 329人阅读 评论(0) 收藏

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

  5. hdu1016 Prime Ring Problem(DFS)

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

  6. Prime Ring Problem dfs

    A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...

  7. Uva 552 Prime Ring Problem(dfs)

    题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...

  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. HDU1016 Prime Ring Problem(DFS回溯)

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

随机推荐

  1. ZooKeeper 典型应用场景-数据发布与订阅

    ZooKeeper 是一个高可用的分布式数据管理与系统协调框架.基于对 Paxos 算法的实现,使该框架保证了分布式环境中数据的强一致性,也正是基于这样的特性,使得 ZooKeeper 可以解决很多分 ...

  2. 108. Convert Sorted Array to Binary Search Tree (building tree with resursion)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...

  3. hdu-1754 I Hate It---线段树模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目大意: 求区间最大值+单点修改 解题思路: 直接套用模板即可 #include<bi ...

  4. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】

    任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...

  5. where are you going ? 反序为:going you are where

    一个反序小算法,就是首尾替换,生成新的反序后的数组

  6. ES6学习笔记(对象)

    1.属性的简洁表示法 const foo = 'bar'; const baz = {foo}; baz // {foo: "bar"} // 等同于 const baz = {f ...

  7. 【luogu P4711 「化学」相对分子质量】 题解

    题目链接:https://www.luogu.org/problemnew/show/P4711 要细心模拟 #include <cstdio> #include <algorith ...

  8. Python—面向对象05 反射

    反射,通过字符串映射到对象属性 ​ class People: country='China' def __init__(self,name,age): self.name=name self.age ...

  9. jzoj5195. 【NOIP2017提高组模拟7.3】A(递推,打表)

    Description

  10. 【PTA 天梯赛训练】电话聊天狂人(简单map)

    输入格式: 输入首先给出正整数N(≤10^5),为通话记录条数.随后N行,每行给出一条通话记录.简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔. 输出格式: 在一行中给出 ...