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. MongoDB 安装 增删改查

    MongoDB   一 介绍 1.高性能的数据存储解决方案是大多数大型Web应用程序和服务的核心.后端数据库负责存储一切东西,从用户账户的信息到购物车中的商品,以及博客和评论数据等.好的Web应用需要 ...

  2. QT 标题栏隐藏可拖拽

    这个也是我网上找到了 为了方便,记录一下 void mousePressEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *e); void ...

  3. mysql新手进阶02

    云想衣裳花想容,春风拂槛露华浓. 若非群玉山头见,会向瑶台月下逢. 现在有一教学管理系统,具体的关系模式如下: Student (no, name, sex, birthday, class) Tea ...

  4. [wirtting] top01 independent

    Do you agree or disagree with the following statement? At universities and colleges, sports and soci ...

  5. Spring 配置String转Date

    操作步骤: 1. 实现 org.springframework.core.convert.converter.Converter 接口 2. 配置 org.springframework.contex ...

  6. vue watch监控对象

    1.普通的watch data() { return { frontPoints: 0 } }, watch: { frontPoints(newValue, oldValue) { console. ...

  7. 洛谷 P1706 全排列问题 :STL / dfs

    题目描述 输出自然数1到n所有不重复的排列,即n的全排列,要求所产生的任一数字序列中不允许出现重复的数字. 输入输出格式 输入格式: n(1≤n≤9) 输出格式: 由1-n组成的所有不重复的数字序列, ...

  8. Apache——访问控制

    Order 指定执行允许访问规则和拒绝访问规则 Deny 定义拒绝访问列表 Allow 定义允许访问列表 Order allow,deny  先执行允许,再执行拒绝 Order deny,allow ...

  9. 【RL系列】Multi-Armed Bandit笔记——UCB策略与Gradient策略

    本篇主要是为了记录UCB策略与Gradient策略在解决Multi-Armed Bandit问题时的实现方法,涉及理论部分较少,所以请先阅读Reinforcement Learning: An Int ...

  10. 自测之Lesson10:管道

    题目:建立双向管道,实现:父进程向子进程传送一个字符串,子进程对该字符串进行处理(小写字母转为大写字母)后再传回父进程. 实现代码: #include <stdio.h> #include ...