DP, the state is F[I][J][K1][K2], for the first person, with J Boys, the suffix of boys more than the number of girls in the maximum value of K1, girls more than the number of boys maximum is k2.k1,k2<=k.
State transition equation F[i][j][k1][k2] = f[i-1][j-1][k1+1][k2-1] + f[i-1][j][k1-1][k2+1]. The premises are legal.
The answer is F[N+M][N][K1][K2]. 0<=k1,k2<=n.
Need to use a scrolling array, the last launch of Next, because swap, became cur, so the output is cur.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMOD =12345678;Const intMAXN = Max+Ten;Const intMAXK = -+Ten;Long Longf[2][maxn][maxk][maxk],res;intN,m,k,cur,next;voidAlterLong Long&x,Long Longy) {x= (x+y)%MOD;}intMain () {scanf ("%d%d%d",&n,&m,&k); Cur=0, next =1; f[0][0][0][0] =1; for(inti =0; I < (N+M); i++, Swap (Cur,next)) {memset (F[next],0,sizeof(F[next])); for(intj =0; J <= min (i,n); J + +) for(intK1 =0; K1 <= min (k,n); k1++) for(intK2 =0; K2 <= min (i-j,k); k2++){ if(K1 < K && J <N) {alter (F[NEXT][J+1][k1+1][max (k2-1,0)],F[CUR][J][K1][K2]); } if(K2 < K && I-j <m) {alter (F[next][j][max (K1-1,0)][k2+1],f[cur][j][k1][k2]); } } } for(inti =0; I <= K; i++) for(intj =0; J <= K; J + +) alter (RES,F[CUR][N][I][J]); printf ("%lld\n", RES); return 0;}
bzoj1037: [ZJOI2008] Birthday party