P2051[AHOI2009] 중국 장기(동적 기획 & 분류 토론)
1356 단어 겨울방학 합숙훈련동적 기획
#include
using namespace std;
typedef long long ll;
const ll MAXN = 105;
const ll MOD = 9999973;
ll dp[MAXN][MAXN][MAXN];
int main()
{
ll n, m, i, j, k;
cin >> n >> m;
dp[0][0][0] = 1;
for(ll i = 1; i <= n; i++)
{
for(j = 0; j <= m; j++)
{
for(k = 0; k <= m - j; k++)
{
dp[i][j][k] = dp[i - 1][j][k];
if(k >= 1)
dp[i][j][k] += dp[i - 1][j + 1][k - 1] * (j + 1);
if(j >= 1)
dp[i][j][k] += dp[i - 1][j - 1][k] * (m - j - k + 1);
if(k >= 2)
dp[i][j][k] += dp[i - 1][j + 2][k - 2] * ((j + 2) * (j + 1) / 2);
if(k >= 1)
dp[i][j][k] += dp[i - 1][j][k - 1] * j * (m - j - k + 1);
if(j >= 2)
dp[i][j][k] += dp[i - 1][j - 2][k] * ((m - j - k + 2) * (m - j - k + 1) / 2);
dp[i][j][k] %= MOD;
}
}
}
ll ans = 0;
for(i = 0; i <= m; i++)
{
for(j = 0; j <= m; j++)
{
ans += dp[n][i][j];
ans %= MOD;
}
}
printf("%lld", (ans + MOD) % MOD);
return 0;
}