UVA529 Addition Chains
Posted mrclr
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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的主要内容,如果未能解决你的问题,请参考以下文章