用b[i][j]表示递推到第i位时数字和为j的方案数,那么总方案数就是b[n][s/2]的平方
1 n, s = input().split(‘ ‘) 2 3 n = int(n) 4 s = int(s) 5 6 if s % 2 == 1: 7 print(0) 8 else: 9 s = int(s // 2) 10 b = [[]] 11 a = [] 12 for i in range(0, 10): 13 a.append(1) 14 for i in range(10, s + 1): 15 a.append(0) 16 b.append(a) 17 for i in range(2, n + 1): 18 a = [0] # 添加一个数用于占位 19 for j in range(0, s + 1): 20 a.append(0) 21 for m in range(0, 10): 22 if m <= j: 23 a[j] += b[i - 1][j - m] 24 b.append(a) 25 print(b[n][s] ** 2)
原文地址:https://www.cnblogs.com/tcctw/p/8642243.html
时间: 2024-10-08 19:55:26