递增数列(迭代加深搜索)

迭代加深搜索就是限制递归的层数,然后一层层地扩大限制的层数

我们记录当前深度,以及当前应该搜出几个数

设计剪枝:
1.当当前深度乘上2^r(r是还没有选的数)比m还小 那肯定是不行的 因为最大的扩展方式就是选两个最大的数
2.这一层比上一层数小

#include<bits/stdc++.h>
using namespace std;
int m,step,s[1000];
bool dfs(int depth)
{
    if(s[depth-1]==m)
    {
        cout<<step<<endl;
        for(int i=1;i<=step;i++)    cout<<s[i]<<" ";
        exit(0);
    }
    if(depth>step)  return false;
    for(register int i=depth-1;i>=1;--i)
    {
        for(register int j=depth-1;j>=i;--j)
        {
            s[depth]=s[i]+s[j];
            if((s[depth]<<(step-depth))<m)  break;
            if(s[depth]<s[depth-1]) break;
            dfs(depth+1);
        }
    } 
    return false;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    cin>>m;
    s[1]=1;
    step=1;
    while(!dfs(2))  ++step; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值