题目描述: 合唱队要求由k个人组成,其中至少包含3个男生2个女生。班级男生n人,女生m人。求所有的组合数(对1000000007求余)。
输入 n, m, k
输出 组合数
输入满足 3<=n<=100 2<=m<=100 5<=k<=m+n
有python写的,本地测试怎么都能通过,提交后一直是0,有大佬答疑解惑吗
class Solution(object):
def __init__(self, n, m):
self.n = n
self.m = m
def get_result(self, k):
count = 0
for x in range(3,k-1):
# x表示男生人数,k-x为女生人数
if x<=self.n and k-x<=self.m:
count += self.get_result2(x, k-x)
print(count%1000000007)
# 计算n个男生,m个女生时的组合数
def get_result2(self, n, m):
count1 = self.C(self.n, n)
count2 = self.C(self.m, m)
return count1*count2
# 计算C(n, m)
def C(self, n, m):
if m>n/2:
m = n-m
res = 1
for i in range(m):
res *= ((n-i)/(i+1))
return int(res)
if __name__ == "__main__":
n, m, k = input().split(' ')
n = int(n)
m = int(m)
k = int(k)
res = Solution(n, m)
res.get_result(k)
def __init__(self, n, m):
self.n = n
self.m = m
def get_result(self, k):
count = 0
for x in range(3,k-1):
# x表示男生人数,k-x为女生人数
if x<=self.n and k-x<=self.m:
count += self.get_result2(x, k-x)
print(count%1000000007)
# 计算n个男生,m个女生时的组合数
def get_result2(self, n, m):
count1 = self.C(self.n, n)
count2 = self.C(self.m, m)
return count1*count2
# 计算C(n, m)
def C(self, n, m):
if m>n/2:
m = n-m
res = 1
for i in range(m):
res *= ((n-i)/(i+1))
return int(res)
if __name__ == "__main__":
n, m, k = input().split(' ')
n = int(n)
m = int(m)
k = int(k)
res = Solution(n, m)
res.get_result(k)
全部评论
(8) 回帖