不知道其他题解在推什么。
下文设 为原题面中的 , 为原题面中的 。
考虑给你 个括号串怎么做。经过一些推导,不难发现答案是 ,其中 和 分别为第 个串的左括号和右括号数量, 为把括号串视为网格图上的折线,起点与最低点的距离, 为终点与最低点的距离。
前者是容易的,枚举每个括号串有多少个左括号即可。答案为 。
后者考虑拆贡献,计算 。所以我们考虑先枚举 ,对单个 算 的方案数。
将最低点设为 ,要求就是起点和终点都 。考虑枚举起点 ,终点 。最低点恰好为 可以容斥掉,即方案数等于经过 的方案数减去经过 的方案数。所以固定 的方案数为 。
答案即为:
发现固定 后正负号抵消,上述式子等于:
这个很明显是组合数后缀和的形式。所以在最外层枚举 ,预处理组合数后缀和,即可 求出上述式子。
时间复杂度 。
#include <bits/stdc++.h>
#define fst first
#define scd second
#define pb emplace_back
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
namespace IO {
const int maxn = 1 << 20;
char ibuf[maxn], *iS, *iT, obuf[maxn], *oS = obuf;
inline char gc() {
return (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, maxn, stdin), (iS == iT ? EOF : *iS++) : *iS++);
}
template<typename T = int>
inline T read() {
char c = gc();
T x = 0;
bool f = 0;
while (c < '0' || c > '9') {
f |= (c == '-');
c = gc();
}
while (c >= '0' && c <= '9') {
x = (x << 1) + (x << 3) + (c ^ 48);
c = gc();
}
return f ? ~(x - 1) : x;
}
inline int reads(char *s) {
char c = gc();
int len = 0;
while (isspace(c)) {
c = gc();
}
while (!isspace(c) && c != -1) {
s[len++] = c;
c = gc();
}
return len;
}
inline void flush() {
fwrite(obuf, 1, oS - obuf, stdout);
oS = obuf;
}
struct Flusher {
~Flusher() {
flush();
}
} AutoFlush;
inline void pc(char ch) {
if (oS == obuf + maxn) {
flush();
}
*oS++ = ch;
}
template<typename T>
inline void write(T x) {
static char stk[64], *tp = stk;
if (x < 0) {
x = ~(x - 1);
pc('-');
}
do {
*tp++ = x % 10;
x /= 10;
} while (x);
while (tp != stk) {
pc((*--tp) | 48);
}
}
template<typename T>
inline void writesp(T x) {
write(x);
pc(' ');
}
template<typename T>
inline void writeln(T x) {
write(x);
pc('\n');
}
}
using IO::read;
using IO::reads;
using IO::write;
using IO::pc;
using IO::writesp;
using IO::writeln;
const int maxn = 4000100;
const ll mod = 1000000007;
const ll inv2 = (mod + 1) / 2;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
ll n, m, a[maxn], fac[maxn], ifac[maxn], pw[maxn], ipw[maxn], f[maxn], g[maxn];
inline ll C(ll n, ll m) {
if (n < m || n < 0 || m < 0) {
return 0;
} else {
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
}
void solve() {
n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
m += a[i];
}
fac[0] = pw[0] = ipw[0] = 1;
for (int i = 1; i <= m + 5; ++i) {
fac[i] = fac[i - 1] * i % mod;
pw[i] = pw[i - 1] * 2 % mod;
ipw[i] = ipw[i - 1] * inv2 % mod;
}
ifac[m + 5] = qpow(fac[m + 5], mod - 2);
for (int i = m + 4; ~i; --i) {
ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
ll ans = 0;
for (int i = 1; i <= n; ++i) {
for (ll j = 0; j <= a[i]; ++j) {
ans = (ans + min(j, a[i] - j) * C(a[i], j) % mod * ipw[a[i]]) % mod;
}
}
for (int i = 1; i <= m; ++i) {
f[i] = 1;
}
for (int i = 1; i <= n; ++i) {
g[a[i] + 1] = 0;
for (int j = a[i]; ~j; --j) {
g[j] = (g[j + 1] + C(a[i], j)) % mod;
}
for (int k = 1; k <= a[i] / 2; ++k) {
ll x = 0;
if ((a[i] + 1) & 1) {
x = (C(a[i], (a[i] + 1) / 2 + k) + g[a[i] / 2 + k + 1] * 2) % mod;
} else {
x = g[(a[i] + 1) / 2 + k] * 2 % mod;
}
f[k] = f[k] * (mod + 1 - x * ipw[a[i]] % mod) % mod;
}
}
for (int i = 1; i <= m; ++i) {
ans = (ans + mod + f[i] - 1) % mod;
}
printf("%lld\n", ans);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}