【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][ ...
随机推荐
- 远程连接阿里云服务器ping不通ip解决方案
搭建了阿里云服务器,发现本地ping不通,查看半天才发现,原来是在阿里云上的安全组少了些东西. 在出入方向上新建一个安全组,就可以搞定了.
- opencv 图像各方向旋转
1. 简介 计算机图形学中的应用非常广泛的变换是一种称为仿射变换的特殊变换,在仿射变换中的基本变换包括平移.旋转.缩放.剪切这几种.本文以及接下来的几篇文章重点介绍一下关于旋转的变换,包括二维旋转变换 ...
- ESX/ESXi 主机的某些存储阵列可能存在读取或写入性能问题 (1002598)
Last Updated: 12/14/2018Categories: Troubleshooting Details 免责声明:本文为 ESX/ESXi hosts might experienc ...
- Map的两种遍历方式
********************************************************************************* ****************** ...
- JS练习:替换式图片自动轮播
代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title ...
- 3D全景之ThreeJs
3D全景之ThreeJs 一.前言 随着H5越来越多的被应用到各个领域,3D也越来越频繁的出现在各个H5案例中,今天我们就来讨论一下3D全景的实现. 据百度百科上介绍:720全景是视角超过人的正常视角 ...
- 【Codeforces 467C】George and Job
[链接] 我是链接,点我呀:) [题意] 让你从1..n这n个数字中 选出来k个不相交的长度为m的区间 然后这个k个区间的和最大 求出这k个区间的和的最大值 [题解] 设dp[i][j]表示前i个数字 ...
- linux的ulimit各种限制之深入分析
一般可以通过ulimit命令或编辑/etc/security/limits.conf重新加载的方式使之生效 通过ulimit比较直接,但只在当前的session有效,limits.conf中可以根据用 ...
- JSON中getInt()和optInt()的区别
最近在用org.json这个包解析json的时候,发现谷歌提供两种不同的数据类型获取方法,比如说针对Int类型,提供了getInt()和optInt()两种方式,谷歌文档中的说明如下: 那么这两者有什 ...
- noip模拟赛 可耻
题目描述 给定一个长度为偶数的排列 p,你每次可以选取 p 排列中相邻的两个元素,假如分别是 x 和 y,那 么把 x 和 y 加入一个序列 q 的末尾,并将 x 和 y 从排列 p 中删除.重复上述 ...