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 ...
随机推荐
- RabbitMQ---5、远程 IP 访问
刚刚安装的RabbitMQ-Server-3.3.7,并且也已经开启了Web管理功能,但是现在存在一个问题: 出于安全的考虑,guest这个默认的用户只能通过http://localhost:1567 ...
- 【转】redis windows环境搭建
一.下载redis windows压缩包 地址参考: https://github.com/ServiceStack/redis-windows/tree/master/downloads https ...
- [javaSE] GUI(菜单)
菜单MenuBar Menu MenuItem 调用Frame对象的setMenuBar()方法,设置菜单,参数:MenuBar对象 import java.awt.FlowLayout; impo ...
- 浅谈Android项目----JSON解析(4种解析技术详解)
json简介 1.概念:json全称是javaScript object Notation,是一种并轻量级的数据交换格式. 2.特点: 1.本质就是具有特定格式的字符串 2.json完全独立于编程语言 ...
- vi 编辑器使用中常见的命令
原创作品,转载请在文章头部(显眼位置)注明出处:https://www.cnblogs.com/sunshine5683/p/10014724.html 要在linux学习的路上越走越远,总结是必不可 ...
- K:找寻数组中第n大的数组元素的三个算法
相关介绍: 给定一个数组,找出该数组中第n大的元素的值.其中,1<=n<=length.例如,给定一个数组A={2,3,6,5,7,9,8,1,4},当n=1时,返回9.解决该问题的算法 ...
- K:hash(哈希)碰撞攻击
相关介绍: 哈希表是一种查找效率极高的数据结构,很多语言都在内部实现了哈希表.理想情况下哈希表插入和查找操作的时间复杂度均为O(1),任何一个数据项可以在一个与哈希表长度无关的时间内计算出一个哈希值 ...
- OpenStack IceHouse 部署 - 4 - 计算节点部署
Nova计算服务(计算节点) 参考 本页内容依照官方安装文档进行,具体参见Configure a compute node(nova service) 前置工作 数据库 由于我们在Nova(计算管理 ...
- java截取字符串
public class Temp { public static void main(String[] args) { String a="dsadgafa34"; System ...
- ioctl
在驱动程序里, ioctl() 函数上传送的变量 cmd 是应用程序用于区别设备驱动程序请求处理内容的值. cmd除了可区别数字外,还包含有助于处理的几种相应信息. cmd的大小为 32位,共分 4 ...