这里是到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)
我只是不明白为什么我的第二种方法错了。你能吗有人告诉我哪里出了问题。你知道吗
这条线可疑:
resw
和friends是阶乘的模逆,所以它们应该相乘而不是相除。最后的结果应该用10**9+7进行修正。你知道吗相关问题 更多 >
编程相关推荐