UVA529 Addition Chains
嘟嘟嘟
还是\(IDA*\)。
这道题是\(ZOJ\)的加强版,\(n\)从\(100\)扩大到了\(10000\),所以必须有非常给力的剪枝才能过。
除了迭代加深,还要加上估价函数:对于当前数\(x\),\(h(x)\)应该是\(O(\log_{2}{x})\),即每一次否给\(x\)乘\(2\)。
然后如果这么手动取乘\(2\)的话,注意得开\(long \ \ long\),要不然会出现负数而\(T\)飞的……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e4 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}
int n, dep = 0, a[100];
bool dfs(int step)
{
  if(step == dep) return a[step] == n ? 1 : 0;
  for(int i = 0; i <= step; ++i)
    for(int j = i; j <= step; ++j)
      {
	if(a[i] + a[j] > n || a[i] + a[j] <= a[step]) continue;
	ll sum = a[i] + a[j];
	for(int k = step + 2; k <= dep; ++k) sum <<= 1;
	if(sum < n) continue;
	a[step + 1] = a[i] + a[j];
	if(dfs(step + 1)) return 1;
      }
  return 0;
}
int main()
{
  while(scanf("%d", &n) && n)
    {
      Mem(a, 0); a[0] = 1;
      int tp = n; dep = 0;
      while(tp >>= 1) dep++;
      for(; !dfs(0); dep++);
      write(a[0]);
      for(int i = 1; i <= dep; ++i) space, write(a[i]); enter;
    }
  return 0;
}
UVA529 Addition Chains的更多相关文章
- 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 ... 
- 1443:【例题4】Addition Chains
		1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ... 
- 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\). 数列中的一 ... 
- UVA 529 - Addition Chains,迭代加深搜索+剪枝
		Description An addition chain for n is an integer sequence with the following four properties: a0 = ... 
- [POJ 2248]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 ... 
- C++解题报告 : 迭代加深搜索之 ZOJ 1937  Addition Chains
		此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ... 
随机推荐
- Transfer-Encoding:chunked 返回数据过长导致中文乱码
			最近在写一个项目的后台时,前端请求指定资源后,返回JSON格式的数据,突然发现在返回的字节数过大时,最后的message中文数据乱码了,对于同一个接口的请求:当数据小时不会乱码,当数据量大了中文就乱码 ... 
- Mongodb操作之查询(循序渐进对比SQL语句)(转http://www.tuicool.com/articles/UzQj6rF)
			工具推荐:Robomongo,可自行百度寻找下载源,个人比较推荐这个工具,相比较mongoVUE则更加灵活. 集合简单查询方法 mongodb语法:db.collection.find() //co ... 
- Java学习--list,set,Map接口使用
			list接口: 泛型:规定list中的元素的类型 /* * * 泛型不能使用基本数据类型(可以使用基本类型的包装类) * */ public void tes ... 
- 【SSH网上商城项目实战13】Struts2实现文件上传功能
			转自:https://blog.csdn.net/eson_15/article/details/51366384 上一节我们做完了添加和更新商品的功能,这两个部分里有涉及到商品图片的上传,并没有详细 ... 
- MVC 导出Execl 的总结几种方式 (二)
			接着上面的来,继续导出Execl 的功能 使用FileResult 方式直接可以生产Execl ,这样我们将会写大量处理后台的代码,个人感觉不好,只是展示出来,提供参考 第一步:编辑控制器 publi ... 
- 使用jQuery获取Dribbble的内容
			Introduction As a web developer, third party API integration is something you will have to face. Esp ... 
- svg矢量图标在html中的使用, (知识点:1.通过h5中的css实现点击变色,2.一个svg文件包含多个图标)
			svg矢量文件体积小,不变形,比传统的png先进,比现在流行的icon-font灵活.然而在使用过程中还是遇到了很多坑.今天花了一天时间把经验整理出来,以供后来者借鉴.如果您从本文收益,请留言mark ... 
- PHP学习笔记(二) ---- PHP数据类型
			PHP __数据结构类型 一.php 中的八种数据类型 1.四种标量类型 Boolean (布尔类型): true or false,多用于条件判断. 实例: <?php $x = &qu ... 
- iOS友盟分享的使用总结
			现在几乎所有的APP都会集成分享,为了可以更好的推广自己的APP. 目前市面上常用的分享无非就三个: 1.友盟分享; 2.shareSDK(mob); 3.苹果原生. 由于苹果原生的分享使用起来不方便 ... 
- python 并发socketserver模块
			1.源码class 1.server类:处理链接 +------------+ | BaseServer | +------------+ | v +-----------+ +----------- ... 
