P2051 [AHOI2009]中国象棋(动态规划&&分类讨论)

洛谷题解:https://www.luogu.org/problemnew/solution/P2051

#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;
}

 

你可能感兴趣的:(寒假集训_动态规划)