LuoGu: CF785D Anton and School - 2
CF: D. Anton and School - 2
钦定最后一个 “(” ,再在前面选择 $i$ 个,后面选择 $i + 1$ 。用范德蒙德卷积。
查看代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| #include <cstdio> #include <cstring> using namespace std; typedef long long L; const int N = 2e5 + 10, mod = 1e9 + 7; char s[N]; int n, f[N], g[N], inv[N], fact[N], ifact[N]; void init () { inv[1] = 1; for (int i = 2; i < N; ++i) inv[i] = -(L)(mod / i) * inv[mod % i] % mod; fact[0] = ifact[0] = 1; for (int i = 1; i < N; ++i) { fact[i] = (L)fact[i - 1] * i % mod; ifact[i] = (L)ifact[i - 1] * inv[i] % mod; } } int C (int a, int b) { return a < b ? 0 : (L)fact[a] * ifact[b] % mod * ifact[a - b] % mod; } int main () { init(); scanf("%s", s + 1); n = strlen(s + 1); for (int i = 1; i <= n; ++i) f[i] = f[i - 1] + (s[i] == '('); for (int i = n; i; --i) g[i] = g[i + 1] + (s[i] == ')'); int res = 0; for (int i = 1; i <= n; ++i) if (s[i] == '(' && g[i]) (res += C(f[i] + g[i] - 1, g[i] - 1)) %= mod; printf("%d", (res + mod) % mod); return 0; }
|