dfs,用全局数组和变量保存并更新当前状态。

答案可以直接在搜索结束时打印,n为奇数时方案数为0。

acm.hdu.edu.cn/showproblem.php?pid=1016
 #include <cstdio>
#include <cstring> using namespace std; const int maxn = ;
int n;
int ans[maxn], k;
bool isprime[];
bool vis[]; void init(){
memset(isprime, , sizeof isprime);
isprime[] = isprime[] = ;
for(int i = ; i <= ; i += ){
bool ok = ;
for(int j = ; j * j <= i; j += ){
if(i % j == ){
ok = ;
break;
}
}
isprime[i] = ok;
}
memset(vis, , sizeof vis);
} void dfs(int u){
//finished num is u
if(u == n && isprime[ + ans[n - ]]){
printf("%d", ans[]);
for(int i = ; i < k; i++) printf(" %d", ans[i]);
printf("\n");
}
for(int i = ; i <= n; i++){
if(!vis[i] && isprime[i + ans[u - ]]){
vis[i] = ;
ans[k++] = i;
dfs(u + );
vis[i] = ;
--k;
}
}
} void solve(){
init();
k = ;
ans[k++] = ;
vis[] = ;
dfs();
} int main(){
int kase = ;
while(~scanf("%d", &n)){
printf("Case %d:\n", ++kase);
solve();
printf("\n");
}
return ;
}

hdu1016 Prime Ring Problem的更多相关文章

  1. HDU1016 Prime Ring Problem(DFS回溯)

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

  2. 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 ...

  3. hdu1016 Prime Ring Problem(DFS)

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

  4. hdu1016 Prime Ring Problem【素数环问题(经典dfs)】

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

  5. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

  6. Prime Ring Problem素数环(HDU1016)

    Prime Ring Problem 思路:先看成一条链,往里头填数,满足任意相邻两数和为质数(这可以打表预处理出40以内的所有质数,扩展的时候枚举),填完了后检查首尾是否满足条件.字典序可以采用扩展 ...

  7. UVA524 素数环 Prime Ring Problem

    题目OJ地址: https://www.luogu.org/problemnew/show/UVA524 hdu oj 1016:  https://vjudge.net/problem/HDU-10 ...

  8. uva 524 prime ring problem——yhx

      Prime Ring Problem  A ring is composed of n (even number) circles as shown in diagram. Put natural ...

  9. hdu 1016 Prime Ring Problem(DFS)

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

随机推荐

  1. 在自定义的UINavigationController中设置背景图片

    //这个方法中设置 + (void)initialize { UINavigationBar *bar = [UINavigationBar appearance]; [bar setBackgrou ...

  2. Leetcode: Evaluate Division

    Equations are given in the format A / B = k, where A and B are variables represented as strings, and ...

  3. 树形DP 2013多校8(Terrorist’s destroy HDU4679)

    题意: There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But no ...

  4. csu oj 1330 字符识别?

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1330 1330: 字符识别? Time Limit: 1 Sec  Memory Limit: 1 ...

  5. C++之路起航——标准模板库(set)

    set(集合):http://baike.baidu.com/link?url=cb68AB-3qfEK8RoaGHJFClb4ZiWpJfc32lPOLtaNUrdxntFC738zCZsCiUlf ...

  6. (转)SQL对Xml字段的操作

    T-Sql操作Xml数据 一.前言 SQL Server 2005 引入了一种称为 XML 的本机数据类型.用户可以创建这样的表,它在关系列之外还有一个或多个 XML 类型的列:此外,还允许带有变量和 ...

  7. 夺命雷公狗---DEDECMS----19dedecms栏目列表页的完成

    我们的栏目页表页的模版如果不清楚叫什么名,可以通过: 这里很清楚的记录着,我们来看下他: 他长得和我们以前做首页时候是不是很像呢?其实就是一样的,我们改下即可,如下所示: {dede:channel ...

  8. 管理科学与工程 国内核心期刊 国外a刊及SCI

    国内: 管理科学与工程: 管理科学学报 A+   (匿名审稿,绝对牛刊,不比一般的SCi期刊的质量差) 系统工程理论与实践 A   (实名审稿,关系稿很多,尤其是挂编委的文章很多,但质量尚可)系统工程 ...

  9. 为archlinux配置cron

    cron的作用:cron可以用来周期性地自动执行一些命令. cron的实现:cron有很多实现版本,例如:cronie, dcron, fcron, bcron, vixie-cron,我安装的是 c ...

  10. 【py网页】urlopen的补充,完美

    urllib 是 python 自带的一个抓取网页信息一个接口,他最主要的方法是 urlopen(),是基于 python 的 open() 方法的.下面是主要说明: 1 urllib.urlopen ...