ZOJ1937:Addition Chains——题解
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——题解的更多相关文章
- 一本通【例题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 ...
- 1443:【例题4】Addition Chains
1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...
- UVA 529 Addition Chains(迭代搜索)
Addition Chains An addition chain for n is an integer sequence with the following four propertie ...
- [POJ2248] Addition Chains 迭代加深搜索
Addition Chains Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5454 Accepted: 2923 ...
- poj 2248 Addition Chains (迭代加深搜索)
[题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...
- 「一本通 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\). 数列中的一 ...
- [POJ 2248]Addition Chains
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
Description An addition chain for n is an integer sequence with the following four properties: a0 = ...
- Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)
An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...
随机推荐
- dubbo入门(一)
1.简介 Dubbo由阿里巴巴开源,是一个分布式服务框架,致力于提供高性能和透明化的RPC(远程过程调用)远程服务调用方案,以及SOA服务治理方案.如果没有分布式的需求,Dbubbo是不需要的,其本质 ...
- Object里面的方法
object里面有12个方法,没写完,写一些部分代表 toString():输出对象的地址字符串(hashcode码) equals():用的是==,比较的是引用,在有些类里面是重写了这个方法的,重写 ...
- [SHELL]退出脚本
一,退出状态码 1,范围:0~255 2,查看退出状态码:必须在命令执行之后立即执行 ,显示的是脚本最后一条命令的退出状态码 echo $? 若f返回值为0,则表示正常 有异常为正值 二,exit 脚 ...
- Windows10安装GPU版本的Tensorflow
本人电脑配置(公司的)gtx1080ti,下载的的cuda8.0,cudnn6.0,python3.5.3安装完成后,安装tensorflow 1.pip install tensorflow-gpu ...
- [Clr via C#读书笔记]Cp8方法
Cp8方法 构造器 作用就是初始化所有成员字段:.ctor:派生类和基类都有自己的构造函数.默认有一个无参数的构造函数,值字段初始化为0,引用字段初始化为null:可以有多个构造器: 值类型的初始化其 ...
- 2.安装hdfs yarn
下载hadoop压缩包设置hadoop环境变量设置hdfs环境变量设置yarn环境变量设置mapreduce环境变量修改hadoop配置设置core-site.xml设置hdfs-site.xml设置 ...
- ZOJ 3689 Digging(DP)
Description When it comes to the Maya Civilization, we can quickly remind of a term called the end o ...
- JavaScript筑基篇(二)->JavaScript数据类型
说明 介绍JavaScript数据类型 目录 前言 参考来源 前置技术要求 JavaScript的6种数据类型 哪6种数据类型 undefined 类型 null 类型 boolean 类型 numb ...
- js经典试题之ES6
js经典试题之ES6 1:在ECMAScript6 中,Promise的状态 答案:pending resolved(fulfilled) rejected 解析: Promise对象只有三种状态: ...
- PHPCMSV9 黄页新闻、产品、商机均无法浏览具体信息,显示您没有访问该信息的权限!
原帖地址:http://bbs.phpcms.cn/forum.php?mod=viewthread&tid=294956&highlight=%C3%BB%D3%D0%B7%C3%C ...