EOJ 2155/UVA 529/POJ 2248/Ulm Local 1997 Addition Chains

https://cn.vjudge.net/problem/UVA-529

容易想到用搜索做.比较难想的是:构造一个a数组记录每个小于n的数要经过多少个数列项之后才能到达n.这样做的好处是便于最优性剪枝:如果发现[当前数列长度]加上[至少还需要的数列长度]已经超过了当前最优解则剪枝.

之后就是按题意常规dfs.

#include <bits/stdc++.h>
using namespace std;

const int maxn = 105;
int n, best, cur[maxn], now[maxn], a[maxn<<1];

void dfs(int dep)
{
    if (dep + a[cur[dep]] >= best) return;
    if (cur[dep] == n)
    {
        best = dep;
        memcpy(now, cur, (dep+1) * sizeof(int));
        return;
    }
    for (int i = dep; i >= 0; --i)
        for (int j = dep; j >= i; --j)
        {
            cur[dep+1] = cur[i] + cur[j];
            if (cur[dep+1] > cur[dep] && cur[dep+1] <= n)
                dfs(dep+1);
        }
}

int main()
{
    while (cin >> n && n)
    {
        for (int i = (n<<1); i >= n; --i) a[i] = 0;
        for (int i = n-1; i >= 1; --i) a[i] = a[i<<1]+1;
        best = 0x3f3f3f3f;
        cur[0] = 1;
        dfs(0);
        for (int i = 0; i < best; ++i)
            printf("%d ", now[i]);
        printf("%d\n", now[best]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值