#深度优先遍历 def rec(i, m, res): ans = [] for j in range(i, n+1): if j == m: ans.extend([x+[j] for x in res]) break if j < m: ans.extend(rec(j+1, m-j, [x+[j] for x in res])) else: continue return ans n,m=list(map(int,input().split())) ans = rec(1, m, [[]]) for a in ans: print(' '.join([str(x) for x in a]))
import traceback
# 回溯法
def backtracing(t, temp, sum, n, m):
if (sum == m):
print(" ".join(map(str, temp)))
else:
for i in range(t + 1, n + 1):
if i + sum <= m:
temp.append(i)
backtracing(i, temp, sum + i, n, m)
temp.pop()
try:
n, m = input().strip().split()
n = int(n)
m = int(m)
backtracing(0, [], 0, n, m)
except:
traceback.print_exc()
DP保存路径。
n,m = map(int,input().split()) ans = [[None for i in range(n+1)] for j in range(m+1)] for i in range(n+1): ans[0][i] = [[]] for i in range(1,m+1): for j in range(1,n+1): if i-j<0: ans[i][j] = ans[i][j-1] else: if ans[i-j][j-1]==None: ans[i][j] = ans[i][j - 1] elif ans[i][j - 1]==None: ans[i][j] = [x+[j] for x in ans[i-j][j-1]] else: ans[i][j] = ans[i][j-1]+[x+[j] for x in ans[i-j][j-1]] ans = ans[-1][-1] for line in sorted([' '.join(map(str,x)) for x in ans]): print(line)
def rec(i, m, res): ans = [] for j in range(i, n+1): if j == m: ans.extend([x+[j] for x in res]) break if j < m: ans.extend(rec(j+1, m-j, [x+[j] for x in res])) else: continue return ans if __name__ == '__main__': n, m = [int(x) for x in input().split()] ans = rec(1, m, [[]]) for a in ans: print(' '.join([str(x) for x in a]))