卡特兰数
板子题一
#define int long long
int jc[maxn], ny[maxn], jcny[maxn];
int C(int n, int m) {
return 1ll * jc[n] * jcny[n - m] % mod * jcny[m] % mod;
}
int lucas(int n, int m) {
if(m == 0) return 1;
return C(n % mod, m % mod) * lucas(n / mod, m / mod) % mod;
}
signed main() {
int n = read(), m = read()
jc[1] = ny[1] = jcny[1] = 1;
for (int i = 2; i <= maxn; i++) jc[i] = jc[i - 1] * i % mod;
for (int i = 2; i <= maxn; i++) ny[i] = (mod - mod / i) * ny[mod % i] % mod;
for (int i = 2; i <= maxn; i++) jcny[i] = (jcny[i - 1] * ny[i]) % mod;
cout << (lucas(n + m, m) - lucas(n + m, m - 1) + mod) % mod << endl;
}
板子题二
#10238. 「一本通 6.6 练习 9」网格
恩,高精,python水过
from math import *
n,m = map(int, input().split())
print(factorial(n + m) // factorial(m) // factorial(n) - factorial(n + m) // factorial(m - 1) // factorial(n + 1))
n, m = map(int, input().split())
def C(n, m):
return jc[n]//jc[n - m]//jc[m]
jc = [1]
for i in range(1, m + n + 1):
jc.append(jc[i - 1] * i)
print(C(n + m, n) - C(n + m, n + 1))