AcWing 886. 求组合数 II
原题链接
简单
作者:
皓首不倦
,
2020-09-04 17:34:07
,
所有人可见
,
阅读 338
'''
预处理所有数值的阶乘和阶乘的逆元,实际计算时候用
预处理的阶乘和逆元的乘法进行
'''
MOD = 1000000007
def pow_mod(a, k, p):
t = []
pow_val = 1 # 2的次幂数, 初始是2^0 = 1
a_pow = a % p # a^(2 ^ i)的数值, 初始是a^(2^0) = a
while pow_val <= k:
t.append(a_pow)
a_pow = (a_pow*a_pow) % p
pow_val <<= 1
ans = 1
for i in range(len(t)):
if k & 1:
ans = (ans * t[i]) % p
k >>= 1
return ans
n = int(input())
arr = []
max_val = 1
for _ in range(n):
a, b = map(int, input().split())
arr.append((a, b))
max_val = max(max_val, a)
t = [(0, 0)] * (max_val+1)
val = 1
for i in range(1, max_val+1):
val *= i
val %= MOD
_val = pow_mod(val, MOD-2, MOD) # val的阶乘的逆元
t[i] = (val, _val)
for a, b in arr:
if a == b:
print(1)
else:
val = t[a][0] * t[a-b][1] * t[b][1]
print(val % MOD)