# KY41 放苹果
# M个苹果,N个盘子,苹果和盘子都没有区别
# 将苹果放入盘子,允许有空盘,问有多少种放法?
# 动态规划解法:
# 令dp[disk][apple] = 苹果apple个,盘子有disk个
# 所有放法中有两种情形:
# 情形一: 不含有空盘,则放法总数为: dp[disk][apple-disk]
# 情形二: 存在至少一个空盘,则放法总数为: dp[disk-1][apple]
# 边界条件: dp[0][0] = 1,
# dp[0][j] = 0, j>0
# dp[i][0] = 1, i>0
dp = [[0 for i in range(21)] for j in range(21)]
dp[0][0] = 1
for diskNum in range(1, 21):
for appleNum in range(0, 21):
if diskNum > appleNum: # disk > apple
dp[diskNum][appleNum] = dp[diskNum-1][appleNum]
else: # disk <= apple
dp[diskNum][appleNum] = dp[diskNum][appleNum-diskNum] + dp[diskNum-1][appleNum]
if __name__ == '__main__':
try:
while True:
appleNum, diskNum = map(int, input().strip().split())
print(dp[diskNum][appleNum])
except EOFError:
pass
KY61 放苹果
最新推荐文章于 2024-08-18 18:02:49 发布