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 ...
随机推荐
- nginx支持php配置
location / { root /wwwroot/phptest; index index.html index.htm index.php; } location ~ \.(php|php5)$ ...
- 在VMware虚拟机下安装Linux CentOS7
1.首先下载并安装VMware虚拟机,下载地址:https://www.vmware.com/cn/products/workstation-pro/workstation-pro-evaluatio ...
- WEB安全--高级sql注入,爆错注入,布尔盲注,时间盲注
1.爆错注入 什么情况想能使用报错注入------------页面返回连接错误信息 常用函数 updatexml()if...floorextractvalue updatexml(,concat() ...
- jmeter 函数助手
1.选项,函数助手对话框,打开函数助手 2.使用方法 输入参数,点击生成,可以直接使用(Name of variable in which to store the result (optional) ...
- 第六模块:WEB框架开发 第1章·Django框架开发88~128
88-Ajax简介 89-Ajax的简单实现 90-基于Ajax的传递数据 91-基于Ajax的登录验证 92-基于Form表单的文件上传 93-请求头之contentType 94-Ajax传递js ...
- Python列表操作大全(非常全)
Python列表操作大全(非常全!!!) 对于python列表的理解可以和C语言里面的数组进行比较性的记忆与对照,它们比较相似,对于python里面列表的定义可以直接用方括号里加所包含对象的方法,并且 ...
- Resharp使用简记
一图流: 拾贝: .字符串引号中直接回车键自动添加连接字符串 Ctrl+B:转到定义 Ctr+F11:展示类结构 Ctr+Alt+j:包围代码块 Ctr+\:注释和取消注释 Alt+Ins:重构 Ct ...
- [CF106C]Buns
面包师Lavrenty打算用馅料做几个面包,然后把它们卖掉. Lavrenty有\(n\)克面团和\(m\)种不同的馅料.馅料种类的下标从\(1到m\),他知道他的第\(i\)种馅料剩下\(a_i\) ...
- 【outPut_Class 输出类】使用说明
对象:outPut 说明:定义输出结果类的相关操作.此对象的核心是[JSON]类,所以它继承了[JSON]类的所有方法 重要: 输出结果样式为XML时,自带根节点"root".输出 ...
- [转载]CENTOS 6.0 iptables 开放端口80 3306 22端口
原文地址:6.0 iptables 开放端口80 3306 22端口">CENTOS 6.0 iptables 开放端口80 3306 22端口作者:云淡风轻 #/sbin/iptab ...