嘟嘟嘟




还是\(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. nodejs添加jsonwebtoken验证

    具体使用模块: 使用compression压缩处理请求响应.cors模块添加跨域.helmet安全模块.body-parser解析请求参数.jsonwebtoken用于生成及校验token.使用内置c ...

  2. JAVA注释方式

    1.单行(single-line)注释    //…… 2.块(block)注释                /*……*/ 3.文档注释                      /**……*/

  3. [javaSE] GUI(图形用户界面)

    java为gui提供的对象都在java.Awt和javax.Swing包中 Awt:抽象窗口工具包,依赖平台,调用系统的本地方法 Swing:完全由java实现的轻量级工具库 eclipse官方封装了 ...

  4. 查看SQLServer数据库表占用大小

      查看数据库mdf 文件中 各个表数据占用大小.      create table #t(name varchar(255), rows bigint, reserved varchar(20), ...

  5. JavaMail获取已发送邮件

    public static void main(String args[]) { Properties props = new Properties(); // 参数配置 props.setPrope ...

  6. oracle数据库字符集和客户端字符集(2%)是不同的,字符集转化可能会造成不可预期的后果

    转载请在文章显眼位置注明出处:https://www.cnblogs.com/sunshine5683/p/10036321.html 今天在plsql连接oracle时候报错提示“数据库字符集和客户 ...

  7. uestc Another LCIS

    Another LCIS Time Limit: 1000 ms Memory Limit: 65536 kB Solved: 193 Tried: 2428 Description For a se ...

  8. Fill Table Row(it’s an IQ test question)

    Here is a table include the 2 rows. And the cells in the first row have been filled with 0~4. Now yo ...

  9. js比较好的一些方法

    js里面有些方法比较容易忘记,但却很实用,很好用的一些方法.在此记录: 1.Math.ceil(x) — 返回大于等于数字参数的最小整数(取整函数),对数字进行上舍入 2.Math.floor(x)– ...

  10. [HttpException (0x80004005): 应用程序已预编译,因此不允许使用目录“/App_Code/”。]

    删除网站下的PrecompiledApp.config文件即可.