AcWing 170. 加成序列
原题链接
简单
作者:
皓首不倦
,
2020-08-05 02:01:53
,
所有人可见
,
阅读 475
'''
迭代加深应用
'''
data = []
def dfs(idx, lim, target):
#print(idx, data)
if idx >= lim:
return 0
if idx == 0:
if target == 1:
data.append(1)
return 1
data.append(1)
return dfs(idx+1, lim, target)
else:
visit = set()
for i in range(idx - 1, -1, -1):
for j in range(i, -1, -1):
new_val = data[i] + data[j]
if new_val == target:
data.append(new_val)
return idx
if new_val <= data[-1]:
continue
if new_val not in visit:
visit.add(new_val)
data.append(new_val)
ans = dfs(idx + 1, lim, target)
if ans != 0:
return ans
data.pop(-1)
return 0
while True:
n = int(input())
if n == 0:
break
dep = 1
while True:
data = []
ans = dfs(0, dep, n)
if ans != 0:
print(' '.join(map(str, data)))
break
dep += 1