把m个同样的苹果放在n个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?
注意:如果有7个苹果和3个盘子,(5,1,1)和(1,5,1)被视为是同一种分法。
数据范围:,。
s=[[0 for i in range(11)]for j in range(11)] for i in range(1,11): for j in range(1,11): if i==j:s[i][j]=s[i][j-1]+1 elif j>i:s[i][j]=s[i][i] else:s[i][j]=s[i-j][j]+s[i][j-1] while True: try: a,b=map(int, input().split()) print(s[a][b]) except: break
import sys # 使用递归方法 def rec_func(m, n): # 如果只有一个盘子,或者只有0-1个苹果,就只有一种方法 if n == 1&nbs***bsp;m == 0&nbs***bsp;m == 1: return 1 # 如果盘子数量大于苹果数量,说明最少有n-m个盘子是空的,可以拿掉n-m个盘子 if n > m: return rec_func(m, m) # 盘子数量小于等于苹果数量 else: # 分两种情况,最少有一个盘子是空的,或者每个盘子至少一个苹果 return rec_func(m, n-1) + rec_func(m-n, n) # 使用动态规划 def dp_func(m, n): # m+1列,n行的列表,用于存储结果 dp = [[0]*(m+1)]*(n+1) # 只有一个盘子时,结果为1 for i in range(m+1): dp[0][i] = 1 dp[1][i] = 1 # 只有0-1个苹果时,结果为1 for i in range(n): dp[i][0] = 1 dp[i][1] = 1 # 从第2行第2列开始遍历 for i in range(2, n+1): for j in range(2, m+1): if i > j: dp[i][j] = dp[j][j] else: dp[i][j] = dp[i-1][j] + dp[i][j-i] return dp[n][m] for line in sys.stdin: m = int(line.split()[0]) n = int(line.split()[1]) # print(rec_func(m, n)) print(dp_func(m, n))
while True: try: m,n = map(int,input().split())#m是苹果数。n是盘子数 a = [[0 for j in range(m+1)]for i in range(n+1)] for i in range(1,n+1): if i == 1: for j in range(1,m+1): a[i][j] = 1 else: for j in range(1,m+1): if j < i: a[i][j] = a[i-1][j] elif j == i: a[i][j] = a[i-1][j]+1 else: a[i][j] = a[i-1][j]+a[i][j-i] print(a[n][m]) except: break
# 思路: # 其实这题考的是数学啊,首先当有0个苹果或者是1个盘子的时候,只有一种分法,而其他情况 # 可以分为两种情况讨论: # 1、m < n,则至少有n - m个盘子是空的,此时就相当于将m个苹果分到m个盘子中,此时(m, n) = (m, m) # 2、m > n, 分法是两种情况分法的和,至少有一个空盘子和没有空盘子,即(m, n) = (m, n - 1) + (m - n, n) def putApple(m, n): if m == 0&nbs***bsp;n == 1: return 1 if n > m: return putApple(m, m) else: return putApple(m, n - 1) + putApple(m - n, n) while True: try: m, n = map(int, input().split()) print(putApple(m, n)) except: break
# 2020年11月14日09:04:38 def divide(m, n): # 0<=m<=10, 1<=n<=10 # 最多只有1个苹果,或只有1个盘子时,只有1种分法 if m <= 1&nbs***bsp;n == 1: return 1 # 至少有2个苹果,且苹果数小于盘子数(一定有空盘) elif m < n: return(divide(m, n-1)) # 至少有2个苹果,且苹果数大于等于盘子数(不一定有空盘) # 1、把m个苹果放在n-1个盘子(有空盘) # 2、先在n个盘子中均放1个苹果(无空盘),再把剩下的m-n个苹果放在n个盘子中 else: return(divide(m, n-1) + divide(m-n, n)) while True: try: data = input().split() m = int(data[0]) n = int(data[1]) print(divide(m, n)) except: break
def helper(plate,apple,start_number): if plate==1: return 1 total_count=0 for j in range(start_number,apple): if j*(plate-1)<=apple-j: total_count+=helper(plate-1,apple-j,j) return total_count while True: try: apple,plate=list(map(int,input().split())) print(helper(plate,apple,0)) except: break
while True: try: m, n = map(int, input().split()) res = 0 def deiapples(k, l): global res if k < 0: return if l <= 1 or k <= 1: res = res + 1 return for i in range(l): x = k - (l-i) deiapples(x, l-i) deiapples(m, min(m, n)) print(res) except: break
def apple_dev_num(m, n): if m == 0 or n == 1: return 1 elif m < n: return apple_dev_num(m, m) else: return apple_dev_num(m, n-1) + apple_dev_num(m-n, n) while True: try: m, n = map(int, input().split()) print(apple_dev_num(m, n)) except: break
# a代表苹果数,b代表盘子数 def func(a,b): # 如果苹果没了,或者只剩一张盘子,返回1 if a == 0&nbs***bsp;b == 1: return 1 # 如果盘子数大于苹果数,最多只能相同数量的盘子,一盘一个 if b > a: return func(a,a) else: # 不然 返回func(a,b-1)代表少放一个盘子的情况,和func(a-b,b)把目前苹果全放入盘子的情况 return func(a,b-1) + func(a-b,b) while True: try: temp = input().split() if temp: a,b = int(temp[0]),int(temp[1]) print(func(a,b)) except: break
def count(m,n):#m为多少个苹果,n为多少个盘子 #1. 盘子多,苹果少,即n>m,count(m,n)=count(m,m) #2. 盘子少,苹果多,即n<=m,又分两种情况: # (1)有至少一个空盘子:count(m,n)=count(m,n-1) # (2)没有空盘子:count(m,n)=count(m-n,n) if m==0 or n==1: return 1 if n>m: return count(m,m) else: return count(m,n-1)+count(m-n,n) while True: try: l=input().split()#['7','3'] m=int(l[0]) n=int(l[1]) print(count(m,n)) except: break
while True: try: m, n = input().strip().split() m, n = int(m), int(n) n = min(m, n) # n > m 与 n == m 相等 dp = [[0] * (n+1) for _ in range(m+1)] # dp[0][0] = 0, dp[0][j] = 1 (for j in [1,n]), dp[i][0] = 0 (for i in [1, m]) for j in range(1, n+1): dp[0][j] = 1 for i in range(1, m+1): for j in range(1, n+1): dp[i][j] = dp[i][j-1] + dp[i-j][j] # print(dp) print(dp[m][n]) except: break