UVA - 1374 Power Calcul

//今天上午优化了一上午的UVA1343, 下午看这个题, 最后也是看的网上的代码。。。。想自己一个一个的做,但是训练计划太紧了。很无奈的感觉。感觉这个题还是需要比较强的思维逻辑能力啊。自己做应该是不行了。。

#include<cstdio>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std;

const int maxn = 2000;
int n, res[maxn];
int cnt[maxn];
int k[32];
int maxd;

int dfs(int d, int cur)
{
    if(d==maxd && cur==n) return true;
    if(d >= maxd || cur > 1500) return false;
    if(cur * k[maxd-d] < n) return false;
    res[d] = cur;
    cnt[cur] = 1;
    for(int i = 0; i <=d; ++i)
    {
        int tmp = res[i] + cur;
        if(!cnt[tmp])
        {
            if(dfs(d+1, tmp)) return true;
        }
        tmp = abs(cur-res[i]);
        if(!cnt[tmp])
        {
            if(dfs(d+1, tmp)) return true;
        }
    }
    cnt[cur] = 0;
    return false;
}

int main()
{
    k[0] = 1;
    for(int i = 1; i <= 31; ++i) k[i] = k[i-1] * 2;
    while(scanf("%d", &n) == 1 && n)
    {
        for(maxd = 0; ; ++maxd)
        {
            memset(cnt,0,sizeof(cnt));
            if(dfs(0, 1)) { printf("%d\n", maxd); break;}
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值