用 Python 实现 C++ 代码:求解组合数学问题
以下是用 Python 实现的代码:
Mod = 998244353
fac = [0] * 55
ifac = [0] * 55
pow = [0] * 55
ans = 0
st = [0] * 55
def Inv(x):
return x if x == 1 else (Mod - Mod // x) * Inv(Mod % x) % Mod
def Gcd(a, b):
return Gcd(b, a % b) if b else a
def DFS(d, c, l):
global ans
if c == 0:
tot = 1
t = 0
for i in range(1, d + 1):
tot = tot * Inv(st[i]) % Mod
for i in range(1, d + 1):
if st[i] != st[i - 1]:
tot = tot * ifac[t] % Mod
t = 1
else:
t += 1
tot = tot * ifac[t] % Mod
for i in range(1, d + 1):
tot = tot * pow[st[i] // 2] % Mod
for i in range(1, d + 1):
for j in range(i + 1, d + 1):
tot = tot * pow[Gcd(st[i], st[j])] % Mod
ans = (ans + tot) % Mod
for i in range(l, c + 1):
st[d + 1] = i
DFS(d + 1, c - i, i)
n, m = map(int, input().split())
pow[0] = fac[0] = ifac[0] = 1
for i in range(1, n + 1):
fac[i] = fac[i - 1] * i % Mod
pow[i] = pow[i - 1] * m % Mod
ifac[i] = Inv(fac[i])
DFS(0, n, 1)
print(ans)
请注意,由于 Python 中没有限制整数范围,因此不需要使用 long long 类型。另外,原代码中使用了 scanf 和 printf 进行输入输出,而 Python 中使用 input 和 print 进行输入输出。
原文地址: https://www.cveoy.top/t/topic/qqFz 著作权归作者所有。请勿转载和采集!