YuCloud

紫菊气,飘庭户,晚烟笼细雨。

《谢新恩·冉冉秋光留不住》 - 五代 - 李煜

UVA529 Addition Chains

//迭代加深
//UVA529 Addition Chains  P110 Addition
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <cmath>

#define ll long long
//#define File
#define ull unsigned long long

using namespace std;

const int N=17;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;

int n;int a[N];int depth;bool f;

void iddfs(int d)
{
    if(d>depth){
        if(a[d-1]==n) f = 1;
        return;
    }
    for(int i=0;i<d;i++){
        for(int j=0;j<d;j++){
            if(a[i]+a[j]>a[d-1] && a[i]+a[j]<=n && !f){
                a[d] = a[i]+a[j];
                int t = a[d];t=t<<(depth - d);
                if(t < n) continue;
                iddfs(d+1);
            }
        }
    }
}

int
main()
{
    #ifdef File
        //freopen(, , stdin);
        //freopen(, , stdout);
    #endif
    a[0]=1;a[1]=2;
    while(scanf("%d",&n)!=EOF && n!= 0){
        if(n==1) {printf("1\n");continue;}
        if(n==2) {printf("1 2\n");continue;}
        f = false ; depth = 1;
        while(!f){
            iddfs(2);
            depth++;
        }
        for(int i=0;i<depth;i++) printf("%d ",a[i]);
        printf("\n");
    }
    return 0;
}
posted @ 2020-10-07 15:36  tree[3]  阅读(81)  评论(0编辑  收藏  举报