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 ...
随机推荐
- mysql 统计连续天数
以下为例子数据 图1 图1 首先根据要求取出BeforeMeal要在7.0以下 并且 bingAfterMeal要在11.1以下 select AccountId,CreateTime from Di ...
- WAKE-WIN10-SOFT-python配置
1,anaconda下载,安装 1,1 官网:https://www.continuum.io/downloads 下载:https://repo.continuum.io/archive/ win1 ...
- php调用含有命名空间的类
现有a.php 和 b.php在同一个目录下 a.php中 namespace myspace; class A{ __construct(){} .... } b.php中调用类A require_ ...
- March 16 2017 Week 11 Thursday
Adventure may hurt you, but monotony will kill you. 也许冒险会让你受伤,但一成不变会让你灭亡. The very theme of the univ ...
- 利用CRM中间件Middleware从ERP下载Customer Material的常见错误
使用事务码VD51和VD52创建和修改Customer Material. 下图是我在ERP创建的Material,为其维护了一个Customer Material AOP. 当下载到CRM后,在We ...
- Android(java)学习笔记44:Map集合的遍历之键值对对象找键和值
1. Map集合的遍历之 键值对对象找 键和值: package cn.itcast_01; import java.util.HashMap; import java.util.Map; impor ...
- bzoj 2434 [Noi2011]阿狸的打字机——AC自动机
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2434 dfs AC自动机,走过的点权值+1,回溯的时候权值-1:走到询问的 y 串的节点,看 ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- EJB 配置多个数据源
1.修改jboss-6.simple\server\default\deploy\transaction-jboss-beans.xml 配置文件 <bean name="CoreEn ...
- vue使用v-for循环,动态修改element-ui的el-switch
在使用element-ui的el-switch中,因为要用v-for循环,一直没有成功,后来仔细查看文档,发现可以这样写 <el-switch v-for="(item, key) i ...