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 ...
随机推荐
- 获取项目中.txt 文件的内容
package com.fh.controller.ruitai.util; import java.io.BufferedInputStream; import java.io.File; impo ...
- 「日常训练」Card Game Cheater(HDU-1528)
题意与分析 题意是这样的:有\(n\)张牌,然后第一行是Adam的牌,第二行是Eve的牌:每两个字符代表一张牌,第一个字符表示牌的点数,第二个表示牌的花色.Adam和Eve每次从自己的牌中选出一张牌进 ...
- web自动化测试框架总结
web自动化测试框架总结: https://www.processon.com/mindmap/5bdab924e4b0878bf41e9e09
- svn清理报错:Cleanup failed to process the following paths
这里碰到svn更新时,提示清理,清理时报错: 只需进行以下几个步骤即可解决:(原理即是清除掉svn数据库里的lock记录) 1.下载SQLiteManager,svn用的是sqlite数据库,需要一款 ...
- python函数参数默认值及重要警告
最有用的形式是对一个或多个参数指定一个默认值.这样创建的函数,可以用比定义时允许的更少的参数调用,比如: def ask_ok(prompt, retries=4, reminder='Please ...
- C 计算时间差
#include <stdio.h>int main(){ //新建四个变量 la 代表小时 kc代表时间 int l,k,a,c; //输入 两个时间 scanf("%d %d ...
- 关于java使用double还是float
眼睛一亮在论坛上发现一枚很有价值的评论赶紧抄下来... 记住java一定要用double,更鼓不变,就算数值不大也要用double.了解java虚拟机的底层会知道,float放在内存中其实是当作dou ...
- svn服务器 备份,迁移,部署方案
这次做业务迁移,要从一个云厂商迁移到某云厂商,之前每天到全备svn排到用场了,需要搭建一个全新到svn服务并要做迁移,并实现我们开发机到时时代码同步 一.svn备份有很多种,优劣都不同,百度可查,我采 ...
- Windows Server 2012四大版本介绍
今天刚好要尝试安装Windows Server 2012,在网上百度了下发现有4个版本,分别是: Datacenter数据中心版. Standard标准版. Essentials版. Foundati ...
- c++源文件到可执行文件的过程
1.预处理(preprocessor):对#pragma.#include.#define.#ifdef/#endif.#ifndef/#endif,inline内联函数等进行处理 2.编译(comp ...