http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1937

题目大意:创造一个数列,使得它:

1.单调不递减。

2.其中一个元素一定存在其前面两个元素之和与它相等。

3.开头为1,结尾为n。

求一个这样的最短数列。

——————————————————————————————————

IDA*一定的(n<=100)

对于IDA*是什么不了解的,可以看我的置顶骑士精神那道题。

我们的估价函数就是当前的值翻多少次二倍后能得到n。

显然假设当前的值为i,则答案为log2(n/i)。

然后就是和普通的IDA*一样做了。

(第1条不要忘了,我就是被这样坑过TAT)

#include<cstdio>
#include<cstring>
#include<cctype>
#include<iostream>
#include<cmath>
using namespace std;
int n,ans,a[];
inline int h(int step){
if(a[step]==n)return -;
return log(n/a[step])/log();
}
inline bool dfs(int step){
if(step==ans){
if(h(step)==-)return ;
return ;
}
if(h(step)+step>ans)return ;
for(int i=step;i>=;i--){
for(int j=i;j>=;j--){
if(a[i]+a[j]>n||a[i]+a[j]<a[step])continue;
a[step+]=a[i]+a[j];
if(dfs(step+))return ;
}
}
return ;
}
int main(){
a[]=;
while(scanf("%d",&n)!=EOF&&n){
for(ans=;;ans++){
if(dfs())break;
}
printf("%d",a[]);
for(int i=;i<=ans;i++)printf(" %d",a[i]);
printf("\n");
}
return ;
}

ZOJ1937:Addition Chains——题解的更多相关文章

  1. 一本通【例题4】Addition Chains——题解

    又是一道剪枝剪了半天的搜索题...题目传送 要充分利用题目中的约束条件:1.:2.对于每个k(1≤k≤m)k(1≤k≤m)满足ak=ai+aj(0≤i,j≤k−1)ak=ai+aj(0≤i,j≤k−1 ...

  2. 1443:【例题4】Addition Chains

    1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...

  3. UVA 529 Addition Chains(迭代搜索)

      Addition Chains  An addition chain for n is an integer sequence  with the following four propertie ...

  4. [POJ2248] Addition Chains 迭代加深搜索

    Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5454   Accepted: 2923   ...

  5. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

  6. 「一本通 1.3 例 4」Addition Chains

    Addition Chains 题面 对于一个数列 \(a_1,a_2 \dots a_{m-1},a_m\) 且 \(a_1<a_2 \dots a_{m-1}<a_m\). 数列中的一 ...

  7. [POJ 2248]Addition Chains

    Description An addition chain for n is an integer sequence with the following four properties: a0 = ...

  8. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  9. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

随机推荐

  1. mybati缓存机制之二级缓存配置

    二级缓存配置 在MyBatis的配置文件中开启二级缓存. <setting name="cacheEnabled" value="true"/> 在 ...

  2. 记录---Testin上新手测试用例设计实战---碎乐3.2.0

    平台上给的版本是碎乐3.12版的,但是平台上给的安装包下载不了,所以加群咨询之后给出了直接去手机应用商店下载搜索到的版本的对策.所以就那应用商店中找到的3.2.0版本来设计测试用例.因为任务中没有给出 ...

  3. flex布局笔记

    flex布局: 容器: 容器主轴方向: 项目的主轴对齐方式: space-between:两端对齐,项目之间的间隔都相等. space-around:每个项目两侧的间隔相等.所以,项目之间的间隔比项目 ...

  4. 【第四章】MySQL数据库的基本操作:数据库、表的创建插入查看

    MySQL数据库基本操作 创建表 create table 查看表结构 desc table, show create table 表完整性约束 修改表 alter table 复制表 create ...

  5. leetcode个人题解——#22 Generate Parentheses

    思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...

  6. lsscsi命令详解

    基础命令学习目录首页 lsscsi包默认是不安装的.lsscsi包安装完之后,lsscsi命令就可以使用了.lsscsi命令(lsscsi -t -L)能很方便的看出哪些是固态硬盘(SSD),哪些是S ...

  7. 【转】Keepalived+Tengine实现高可用集群

    原文出处:http://502245466.blog.51cto.com/7559397/1301772 概述 近年来随着Nginx在国内的发展潮流,越来越多的互联网公司使用Nginx:凭Nginx的 ...

  8. Median Weight Bead(最短路—floyed传递闭包)

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  9. wwnjld第二轮迭代测试报告

    1.引言 1.1测试报告目的 被测试报告为wwnjld小组我们的时间管理软件的第二轮迭代所写的软件测试报告.在经过本小组大家不懈的努力之下,我们小组第二轮迭代的产品终于新鲜出炉了.这次测试小组的主要成 ...

  10. lintcode-28-搜索二维矩阵

    搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1, 3, ...