标签:避免 长度 ios names fail get pre lse puts
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int n,dep,a[10005];
inline bool dfs(int now){
if(now>dep){
if(a[dep]==n)return true;
return false;
}
if(a[now-1]*(1<<(dep-now+1))<n)return false;//剪枝4
map<int,int>fail;//剪枝3
for(int i=now-1;i>=1;--i){//剪枝1的倒序枚举
if(a[i]+a[i]<=a[now-1])break;//剪枝2
for(int j=i;j>=1;--j){//避免重复,从第i位开始
if(a[i]+a[j]<=a[now-1])break;//剪枝2
if(a[i]+a[j]<=n&&!fail[a[i]+a[j]]){
a[now]=a[i]+a[j];
if(dfs(now+1))return true;
fail[a[i]+a[j]]=1;//剪枝3的标记操作
a[now]=0;
}
}
}
return false;
}
int main(){
while(1){
n=read();if(!n)break;
if(n==1){puts("1");continue;}
if(n==2){printf("1 2\n");continue;}
a[1]=1;a[2]=2;dep=3;
while(1){//迭代加深,从3开始
if(dfs(3))break;
++dep;
}
for(int i=1;i<dep;++i)printf("%d ",a[i]);
printf("%d\n",a[dep]);
}
return 0;
}
标签:避免 长度 ios names fail get pre lse puts
原文地址:https://www.cnblogs.com/PPXppx/p/11388809.html