给定 \(n\) 个 0 和 \(n\) 个 1,它们按照某种顺序排成长度为 \(2n\) 的序列,满足任意前缀中 0 的个数都不少于 1 的个数的序列的数量为:\(Cat(n) = C_{2n}^n - C_{2n}^{n - 1} = \frac{C_{2n}^n}{n + 1}\)
#include <bits/stdc++.h> using namespace std; #define LL long long const int mod = 1e9 + 7; LL n; LL qmi(LL a, LL k, LL p){ LL ans = 1; while (k){ if (k & 1) ans = ans * a % p; a = a * a % p; k >>= 1; } return ans; } int main(){ cin >> n; LL a = 2 * n, b = n, ans = 1; for (int i = a; i > a - b; i -- ) ans = ans * i % mod; for (int i = 1; i <= b; i ++ ) ans = ans * qmi(i, mod - 2, mod) % mod; ans = ans * qmi(n + 1, mod - 2, mod) % mod; cout << ans << "\n"; return 0; }