「TJOI2019」唱、跳、rap 和篮球
题目分析
据说这是一道生成函数题
看到限制条件,我们首先想到的就是对有多少组讨论cxk的人进行容斥。然后就是求剩下的人随便放有多少种方法了。考虑现在每种剩\(a,b,c,d\)人,还需要排\(n\)人,那么方案数就是
\[\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^c\sum_{l=1}^d [i+j+k+l=n]{n\choose i}{n - i\choose j}{n-i-j\choose k}{n-i-j-k\choose l}
\]
其中\([]\)内表达式为真时,值为\(1\),否则为\(0\)。
可以化简为
\[\sum_{i=1}^a\sum_{j=1}^b\sum_{k=1}^c[0\leqslant n-i-j-k\leqslant d]{n\choose i}{n-i\choose j}{n-i-j \choose k}
\]
这个式子暴力求得话复杂度明显不对,他是\(O(n^3)\)的。
考虑只枚举\(i\)和\(j\)。那么满足\(0\leqslant n-i-j-k\leqslant d\)的\(k\)显然是一个连续的区间,不妨记作\([L,R]\)。那么对答案的贡献就是
\[{n\choose i}{n-i\choose j}\sum_{k=L}^R{n-i-j\choose k}
\]
后面那个求和预处理加前缀和即可\(O(1)\)求得。那么时间复杂度就降到了\(O(n^2)\)。加上外面一层容斥,那么总时间复杂度就是\(O(n^3)\)。
实现的时候注意边界条件!!!
参考程序
码风氪化
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const LL Maxn = 1010;
const LL Maxa = 510;
const LL Mod = 998244353;
LL N, A[ 4 ], Fact[ Maxn ], Inv[ Maxn ], C[ Maxn ][ Maxn ], Sum[ Maxn ][ Maxn ], Ans;
void Exgcd( LL a, LL b, LL &x, LL &y ) { if( b == 0 ) { x = 1; y = 0; return; } Exgcd( b, a % b, y, x ); y -= a / b * x; return; }
LL inv( LL a ) { LL x, y; Exgcd( a, Mod, x, y ); if( x < 0 ) x += Mod; return x; }
LL S( LL n, LL l, LL r ) { LL Ans = Sum[ n ][ r ]; if( l ) Ans = ( Ans - Sum[ n ][ l - 1 ] + Mod ) % Mod; return Ans; }
LL Cal( LL Sta ) {
LL Ans = 0; for( LL i = 0; i < 4; ++i ) A[ i ] -= Sta;
for( LL i = 0; i <= A[ 0 ] && i <= N - Sta * 4; ++i )
for( LL j = max( 0LL, N - Sta * 4 - A[ 2 ] - A[ 3 ] - i ); j <= A[ 1 ] && i + j <= N - Sta * 4; ++j ) {
Ans = ( Ans + C[ N - Sta * 4 ][ i ] * C[ N - Sta * 4 - i ][ j ] % Mod * S( N - Sta * 4 - i - j, max( 0LL, N - Sta * 4 - i - j - A[ 3 ] ), min( N - Sta * 4 - i - j, A[ 2 ] ) ) % Mod ) % Mod;
}
for( LL i = 0; i < 4; ++i ) A[ i ] += Sta; return Ans;
}
void Work() {
for( LL i = 0; i <= N / 4; ++i ) {
for( LL j = 0; j < 4; ++j ) if( i > A[ j ] ) return;
if( i & 1 ) Ans = ( Ans - C[ N - i * 3 ][ i ] * Cal( i ) % Mod + Mod ) % Mod;
else Ans = ( Ans + C[ N - i * 3 ][ i ] * Cal( i ) % Mod ) % Mod;
}
return;
}
int main() {
scanf( "%lld", &N ); for( LL i = 0; i < 4; ++i ) scanf( "%lld", &A[ i ] );
Fact[ 0 ] = 1; for( LL i = 1; i <= N; ++i ) Fact[ i ] = Fact[ i - 1 ] * i % Mod;
Inv[ N ] = inv( Fact[ N ] ); for( LL i = N - 1; i >= 0; --i ) Inv[ i ] = Inv[ i + 1 ] * ( i + 1 ) % Mod;
for( LL i = 0; i <= N; ++i ) for( LL j = 0; j <= i; ++j ) C[ i ][ j ] = Fact[ i ] * Inv[ j ] % Mod * Inv[ i - j ] % Mod;
for( LL i = 0; i <= N; ++i ) for( LL j = 0; j <= i; ++j ) Sum[ i ][ j ] = ( Sum[ i ][ j - 1 ] + C[ i ][ j ] ) % Mod;
Work(); printf( "%lld\n", Ans );
return 0;
}