Prime Ring Problem (DFS练习题)
K - Prime Ring Problem
#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练习题)的更多相关文章
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem(dfs水)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1016 Prime Ring Problem Time Limit: 4000/2000 MS (Jav ...
- 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 ...
- hdu1016 Prime Ring Problem(DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Prime Ring Problem dfs
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ..., n into each circle ...
- Uva 552 Prime Ring Problem(dfs)
题目链接:Uva 552 思路分析:时间限制为3s,数据较小,使用深度搜索查找所有的解. 代码如下: #include <iostream> #include <string.h&g ...
- HDU 1016 Prime Ring Problem(经典DFS+回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU1016 Prime Ring Problem(DFS回溯)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
随机推荐
- ZooKeeper 典型应用场景-数据发布与订阅
ZooKeeper 是一个高可用的分布式数据管理与系统协调框架.基于对 Paxos 算法的实现,使该框架保证了分布式环境中数据的强一致性,也正是基于这样的特性,使得 ZooKeeper 可以解决很多分 ...
- 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 ...
- hdu-1754 I Hate It---线段树模板题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1754 题目大意: 求区间最大值+单点修改 解题思路: 直接套用模板即可 #include<bi ...
- BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 【分块】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=2002 2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 ...
- where are you going ? 反序为:going you are where
一个反序小算法,就是首尾替换,生成新的反序后的数组
- ES6学习笔记(对象)
1.属性的简洁表示法 const foo = 'bar'; const baz = {foo}; baz // {foo: "bar"} // 等同于 const baz = {f ...
- 【luogu P4711 「化学」相对分子质量】 题解
题目链接:https://www.luogu.org/problemnew/show/P4711 要细心模拟 #include <cstdio> #include <algorith ...
- Python—面向对象05 反射
反射,通过字符串映射到对象属性 class People: country='China' def __init__(self,name,age): self.name=name self.age ...
- jzoj5195. 【NOIP2017提高组模拟7.3】A(递推,打表)
Description
- 【PTA 天梯赛训练】电话聊天狂人(简单map)
输入格式: 输入首先给出正整数N(≤10^5),为通话记录条数.随后N行,每行给出一条通话记录.简单起见,这里只列出拨出方和接收方的11位数字构成的手机号码,其中以空格分隔. 输出格式: 在一行中给出 ...