Solution -「多校联训」消失的运算符
\(\mathcal{Description}\)
Link.
给定长度为 \(n\) 的合法表达式序列 \(s\),其中数字仅有一位正数,运算符仅有 -
作为占位。求将其中恰好 \(k\) 个 -
替换为 +
,其余 -
替换为 *
的所有方案得到的表达式结果之和。答案模 \((10^9+7)\)。
\(n\le10^5\)(可能有无意义的多层括号嵌套),-
的总数 \(m\le2.5\times10^3\)。
\(\mathcal{Solution}\)
复杂表达式问题,应当考虑按匹配括号建树,并在树上 DP。
考虑树上结点 \(u\) 以及它的孩子们 \(v_1,\cdots,v_c\),它们代表了形如 u=(v1)-(v2)-...-(vc)
的表达式。令 \(f_{u,i}\) 表示 \(u\) 从左到右合并了一些孩子,用了 \(i\) 个 +
时,所有表达式结果之和;\(g_i\) 表示 \(u\) 从左到右合并了一些孩子,用了 \(i\) 个 +
时,所有后缀乘积之和。设已合并的孩子们共有 \(s_u\) 个 +
,现欲合并孩子 \(v\),其有 \(s_v\) 个 +
,转移分 +
, *
讨论:
\[\begin{aligned}f'_{u,i+j+1}&\longleftarrow^+\binom{s_v}{j}f_{u,i}+\binom{su}{i}f_{v,j},\\f'_{u,i+j}&\longleftarrow^+\binom{s_v}{j}(f_{u,i}-g_i)+g_if_{v,i},\\g'_{i+j+1}&\longleftarrow^+\binom{s_u}{i}f_{v,j},\\g'_{i+j}&\longleftarrow^+g_if_{v,j},\\s_u&\longleftarrow^+s_v+1.\end{aligned}
\]
转移复杂度是树上背包,总复杂度 \(\mathcal O(n+m^2)\)。
\(\mathcal{Code}\)
/*~Rainybunny~*/
#pragma GCC optimize( "Ofast" )
#include <bits/stdc++.h>
#define rep( i, l, r ) for ( int i = l, rep##i = r; i <= rep##i; ++i )
#define per( i, r, l ) for ( int i = r, per##i = l; i >= per##i; --i )
const int MAXN = 2e5, MAXM = 5e3, MOD = 1e9 + 7;
int n, k, m, node, siz[MAXM + 5], mtc[MAXN * 3 + 5];
int comb[MAXM + 5][MAXM + 5], f[MAXM + 5][MAXM + 5];
char str[MAXN * 3 + 5], tmps[MAXN + 5];
inline int imin( const int u, const int v ) { return u < v ? u : v; }
inline int imax( const int u, const int v ) { return u < v ? v : u; }
inline int mul( const int u, const int v ) { return 1ll * u * v % MOD; }
inline int sub( int u, const int v ) { return ( u -= v ) < 0 ? u + MOD : u; }
inline int add( int u, const int v ) { return ( u += v ) < MOD ? u : u - MOD; }
inline void addeq( int& u, const int v ) { ( u += v ) >= MOD && ( u -= MOD ); }
inline void init() {
int len = n; n = 0;
rep ( i, 1, len ) {
if ( '0' <= tmps[i] && tmps[i] <= '9' ) {
str[++n] = '(', str[++n] = tmps[i], str[++n] = ')';
} else {
str[++n] = tmps[i];
m += tmps[i] == '-';
}
}
static int stk[MAXN * 3 + 5]; int top = 0;
rep ( i, 1, n ) {
if ( str[i] == '(' ) stk[++top] = i;
else if ( str[i] == ')' ) mtc[mtc[stk[top]] = i] = stk[top], --top;
}
comb[0][0] = 1;
rep ( i, 1, m ) {
comb[i][0] = 1;
rep ( j, 1, i ) comb[i][j] = add( comb[i - 1][j - 1], comb[i - 1][j] );
}
}
inline int solve( int l, int r ) {
if ( mtc[l] == r ) return solve( l + 1, r - 1 );
int u = ++node, *fu = f[u];
if ( l == r ) return fu[0] = str[l] ^ '0', u;
int g[MAXM + 5];
for ( int p = l, v, fir = true; p < r; ) {
if ( str[p] != '(' ) { ++p; continue; }
const int *fv = f[v = solve( p, mtc[p] )]; p = mtc[p] + 1;
if ( fir ) {
rep ( i, 0, siz[v] ) fu[i] = g[i] = fv[i];
siz[u] = siz[v], fir = false;
continue;
}
static int tmp[MAXM + 5];
rep ( i, 0, siz[u] ) rep ( j, 0, siz[v] ) {
addeq( tmp[i + j + 1], add( mul( fu[i], comb[siz[v]][j] ),
mul( fv[j], comb[siz[u]][i] ) ) );
addeq( tmp[i + j], add( mul( sub( fu[i], g[i] ),
comb[siz[v]][j] ), mul( g[i], fv[j] ) ) );
}
rep ( i, 0, siz[u] + siz[v] + 1 ) fu[i] = tmp[i], tmp[i] = 0;
rep ( i, 0, siz[u] ) rep ( j, 0, siz[v] ) {
addeq( tmp[i + j], mul( g[i], fv[j] ) );
addeq( tmp[i + j + 1], mul( comb[siz[u]][i], fv[j] ) );
}
rep ( i, 0, siz[u] + siz[v] + 1 ) g[i] = tmp[i], tmp[i] = 0;
siz[u] += siz[v] + 1;
}
return u;
}
int main() {
freopen( "operator.in", "r", stdin );
freopen( "operator.out", "w", stdout );
scanf( "%d %d %s", &n, &k, tmps + 1 );
init(); //, fprintf( stderr, "%s\n", str + 1 );
printf( "%d\n", f[solve( 1, n )][k] );
return 0;
}