N有多少种不同的有序组合

2024-09-30 01:22:38 发布

您现在位置:Python中文网/ 问答频道 /正文

这里是到spoj问题http://www.spoj.com/problems/GOODB/的链接。 你得到N, WA, TLE, RE这样

N = WA+TLE+RE

存在多少不同的N不正确结果的有序组合(每个都是WA、TLE或RE)来满足它们的预测?你知道吗

这是我在python中的工作解决方案

import math
mod=10**9+7
def nck(n,k):
    return math.factorial(n)/(math.factorial(n-k) * math.factorial(k))

n,w,t,r = map(int, raw_input().split())
print (nck(n,w) * nck(n-w, t)) % mod

这里是第二种方法,对于同样的问题,通过假设的方法来安排数量 n1 a1’s,n2 a2’s,…,nk ak’s在一排是

n!/(n1! n2! .... nk !)

,在哪里

n = n1 + n2 +...+ nk 

这是我第二种方法的代码

def modexp(x, y, mod):
    res = 1
    if x==0 or x==1:
        return 1
    while y != 0:
        if y & 1 == 1:
            'if y is odd'
            res = (res * x) % mod
        x = (x * x) % mod
        y >>= 1
   return res

def modfact(n, mod):
    res = 1
    while n >= 1:
        res = (res * n) % mod
        n -= 1
    return res

mod = 10 ** 9 + 7
n, w, t, r = map(int, input().split())
resn = modfact(n, mod)
resw = modexp(modfact(w, mod), mod - 2, mod)
rest = modexp(modfact(t, mod), mod - 2, mod)
resr = modexp(modfact(r, mod), mod - 2, mod)
res = (resn*resw*rest*resr)%mod
print(res)

我只是不明白为什么我的第二种方法错了。你能吗有人告诉我哪里出了问题。你知道吗


Tags: 方法remodreturndefresmathwa

热门问题