嘟嘟嘟




还是\(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的更多相关文章

  1. UVA 529 Addition Chains(迭代搜索)

      Addition Chains  An addition chain for n is an integer sequence  with the following four propertie ...

  2. [POJ2248] Addition Chains 迭代加深搜索

    Addition Chains Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5454   Accepted: 2923   ...

  3. 1443:【例题4】Addition Chains

    1443:[例题4]Addition Chains 题解 注释在代码里 注意优化搜索顺序以及最优化剪枝 代码 #include<iostream> #include<cstdio&g ...

  4. poj 2248 Addition Chains (迭代加深搜索)

    [题目描述] An addition chain for n is an integer sequence with the following four properties: a0 = 1 am ...

  5. 「一本通 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\). 数列中的一 ...

  6. UVA 529 - Addition Chains,迭代加深搜索+剪枝

    Description An addition chain for n is an integer sequence  with the following four properties: a0 = ...

  7. [POJ 2248]Addition Chains

    Description An addition chain for n is an integer sequence with the following four properties: a0 = ...

  8. Addition Chains POJ - 2248 (bfs / dfs / 迭代加深)

    An addition chain for n is an integer sequence <a0, a1,a2,...,am=""> with the follow ...

  9. C++解题报告 : 迭代加深搜索之 ZOJ 1937 Addition Chains

    此题不难,主要思路便是IDDFS(迭代加深搜索),关键在于优化. 一个IDDFS的简单介绍,没有了解的同学可以看看: https://www.cnblogs.com/MisakaMKT/article ...

随机推荐

  1. 【10】Quartz.net 定时服务实例

    一.安装nuget包 Install-Package Quartz Install-Package Common.Logging.Log4Net1211 Install-Package log4net ...

  2. 对自写的Asp.Net分页控件的应用方式(异步无刷新分页)

    前台代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" co ...

  3. UVA1584(环状序列)

    对于序列的最小值,可以定义一个比较的方法,然后用一般找最小值的方法遍历一遍即可 #include <iostream> #include <string> #include & ...

  4. PHP 经典算法

    <?  //--------------------  // 基本数据结构算法 //--------------------  //二分查找(数组里查找某个元素)  function bin_s ...

  5. C#学习笔记(基础知识回顾)之值类型与引用类型转换(装箱和拆箱)

    一:值类型和引用类型的含义参考前一篇文章 C#学习笔记(基础知识回顾)之值类型和引用类型 1.1,C#数据类型分为在栈上分配内存的值类型和在托管堆上分配内存的引用类型.如果int只不过是栈上的一个4字 ...

  6. sql: postgreSQL sql script

    SELECT * from pg_class c,pg_attribute a,pg_type t where c.relname='BookKindList' and a.attnum>0 a ...

  7. git杂记-撤销操作

    覆盖上一次的提交或重新更新提交说明 $ git commit --amend -m '我再次提交啦,上一次的提交已经不见啦.这是一个危险的操作哦.哈哈,其实并不危险,也是可以数据恢复的啦' 取消已暂存 ...

  8. BZOJ1996 [Hnoi2010] 合唱队

    Description Input Output Sample Input 4 1701 1702 1703 1704 Sample Output 8 HINT Solution 令$f_{i,j}$ ...

  9. wget 抓取整站

    wget -r -p -np -k http://doc.code365.net/Manual/FreeBSD_Arch_HandBook/ -r 递归 -p, --page-requisites(页 ...

  10. 转:使用VS Code断点调试PHP

    使用VS Code断点调试PHP vs code 使用一款杰出的轻量级代码编辑器,其中的插件工具不胜枚举而且还在不断增加.使用 vs code 调试 PHP 代码更是方便简洁,下面我们来一起看一下. ...