【POJ 2248】 Addition Chain
【题目链接】
http://poj.org/problem?id=2248
【算法】
搜索剪枝
剪枝1 : 优化搜索顺序,从大到小枚举
剪枝2 : Ai + Aj可能相等,只需搜一次即可
剪枝3 : 通过观察发现 : m <= 10,可以用迭代加深搜索
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std; int i,n,step;
int ans[]; inline bool dfs(int dep)
{
int i,j;
bool visited[];
if (dep > step)
{
if (ans[step] == n) return true;
else return false;
} else
{
memset(visited,false,sizeof(visited));
for (i = dep - ; i >= ; i--)
{
for (j = i; j >= ; j--)
{
if (ans[i] + ans[j] <= ans[i-]) break;
if (ans[i] + ans[j] > n || visited[ans[i]+ans[j]]) continue;
visited[ans[i]+ans[j]] = true;
ans[dep] = ans[i] + ans[j];
if (dfs(dep+)) return true;
}
}
return false;
}
} int main()
{ while (scanf("%d",&n) != EOF && n)
{
ans[] = ;
for (i = ; i <= ; i++)
{
step = i;
if (dfs()) break;
}
for (i = ; i <= step; i++) printf("%d ",ans[i]);
printf("\n");
} return ; }
【POJ 2248】 Addition Chain的更多相关文章
- 【POJ 2750】 Potted Flower(线段树套dp)
[POJ 2750] Potted Flower(线段树套dp) Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4566 ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
- BZOJ2292: 【POJ Challenge 】永远挑战
2292: [POJ Challenge ]永远挑战 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 513 Solved: 201[Submit][ ...
随机推荐
- ARM架构与体系学习(二)——3级流水线
ARM架构与体系学习(二)——3级流水线 标签: 存储嵌入式汇编c 2012-04-18 00:44 5414人阅读 评论(4) 收藏 举报 分类: ARM7(16) 版权声明:本文为博主原创文章 ...
- POJ_3013_最短路
Big Christmas Tree Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 23630 Accepted: 5 ...
- Android中Application类总结
本文出处: 炎之铠csdn博客:http://blog.csdn.net/totond 炎之铠邮箱:yanzhikai_yjk@qq.com 本文原创,转载请注明本出处! 前言 最近的开发中经常使用到 ...
- EF-Linq
一丶基本语法(from a in Table where a.id="001" select a).Tolist(); 隐式内连接from a in table1 join b i ...
- LeetCode--寻找数组中心索引
给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和. 如果数组不存在中心索引,那么我 ...
- LOJ 6145 Easy (动态点分治+线段树)
题目传送门 先建出来点分树,以每个点为根开线段树,维护点分子树内编号为$[l,r]$的儿子到根的距离最小值 每次查询$x$开始,沿着点分树向上跑,在每个点的线段树的$[l,r]$区间里都查一遍取$mi ...
- MySQL详细操作
一.用户管理 -- 创建用户 create user "用户名"@"IP地址" identified by "密码"; "; &q ...
- Linux - redis持久化RDB与AOF
目录 Linux - redis持久化RDB与AOF RDB持久化 redis持久化之AOF redis不重启,切换RDB备份到AOF备份 确保redis版本在2.2以上 实验环境准备 备份这个rdb ...
- 关于嵌入式web服务器
1.boa的配置文件boa.conf Port 80 //服务访问端口 User 0 Group 0 ErrorLog /var/log/boa/error_log //错误日志地址 AccessLo ...
- play snake on windows
今天和人吃晚饭突然想起来 之前西佳佳老师说小学期会要求两星期撸一个小游戏 有人已经撸完一个俄罗斯方块了... 菜逼我决定从最简单的贪吃蛇玩起... 我是直接参考的这个博客 算是相当简单而且很Low的实 ...