首页 > 其他 > 详细

洛谷P2051 [AHOI2009]中国象棋(dp)

时间:2019-03-17 22:43:40      阅读:155      评论:0      收藏:0      [点我收藏+]

题面

luogu

题解

\(50pts:\)显然是\(3\)进制状压\(dp\)

\(100pts:\)

一行一行地考虑

\(f[i][j][k]\)表示前\(i\)行,有\(j\)列放了一个,有\(k\)列放了两个的方案数

转移很显然

Code

#include<bits/stdc++.h>

#define LL long long
#define RG register

using namespace std;
template<class T> inline void read(T &x) {
    x = 0; RG char c = getchar(); bool f = 0;
    while (c != '-' && (c < '0' || c > '9')) c = getchar(); if (c == '-') c = getchar(), f = 1;
    while (c >= '0' && c <= '9') x = x*10+c-48, c = getchar();
    x = f ? -x : x;
    return ;
}
template<class T> inline void write(T x) {
    if (!x) {putchar(48);return ;}
    if (x < 0) x = -x, putchar('-');
    int len = -1, z[20]; while (x > 0) z[++len] = x%10, x /= 10;
    for (RG int i = len; i >= 0; i--) putchar(z[i]+48);return ;
}
const int N = 110, Mod = 9999973;
int n, m;
int f[N][N][N];
void pls(int &x, LL y) {
    y %= Mod;
    x += y;
    if (x >= Mod) x -= Mod;
}

int C(int x) {//C(x, 2)
    return x * (x - 1) / 2;
}

int main() {
    read(n), read(m);
    f[0][0][0] = 1;
    for (int i = 0; i < n; i++)//主动转移
        for (int j = 0; j <= m; j++)
            for (int k = 0; k + j <= m; k++)
                if (f[i][j][k]) {
                    pls(f[i + 1][j][k], f[i][j][k]);
                    if (m - k - j) pls(f[i + 1][j + 1][k], 1ll * f[i][j][k] * (m - k - j));
                    if (j) pls(f[i + 1][j - 1][k + 1], 1ll * f[i][j][k] * j);
                    if (m - k - j > 1) pls(f[i + 1][j + 2][k], 1ll * f[i][j][k] * C(m - k - j));
                    if (j > 1) pls(f[i + 1][j - 2][k + 2], 1ll * f[i][j][k] * C(j));
                    if (m - k - j && j) pls(f[i + 1][j][k + 1], 1ll * f[i][j][k] * j * (m - k - j));
                }
    int ans = 0;
    for (int j = 0; j <= m; j++)
        for (int k = 0; k + j <= m; k++)
            pls(ans, f[n][j][k]);
    printf("%d\n", ans);
    return 0;
}

洛谷P2051 [AHOI2009]中国象棋(dp)

原文:https://www.cnblogs.com/zzy2005/p/10549410.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!