萌新求助,迭代加深dfs,TLE
查看原帖
萌新求助,迭代加深dfs,TLE
421265
eastcloud楼主2022/4/16 15:11
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
ll n,cho[11],lim;
inline int read(){
	int x=0,ff=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')ff=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}
	return ff*x;
}
bool dfs(ll x){
	if(x>lim) return cho[x-1]==n;
	for(int i=x-1;i>=1;i--){
		ll tmp=cho[x-1]+cho[i];
		cho[x]=tmp;
		if(tmp>n) continue;
		for(int i=x+1;i<=lim;i++)tmp*=2;
		if(tmp<n) return false;
		if(dfs(x+1))return true;
		cho[x]=0;
	}
	return false;
}
int main(){
	cin>>n;
	while(n){
		if(n==1){
			cout<<1<<endl;
			cin>>n;
			continue;
		}
		lim=2;
		cho[1]=1;
		while(!dfs(2))lim++;
		for(int i=1;i<=lim;i++){
			if(i==lim) cout<<cho[i]<<endl;
			else cout<<cho[i]<<' ';
		}
		n=read();
	}
    return 0;
}
2022/4/16 15:11
加载中...