HDU 4832(DP+计数问题)

HDU 4832 Chess

思路:把行列的情况分别dp求出来,然后枚举行用几行,竖用几行,然后相乘累加起来就是答案

代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

typedef long long ll;

const ll MOD = 9999991;
const int N = 1005;
int t, n, m, k, x, y;
ll dp1[N][N], dp2[N][N], C[N][N];

int main() {
    for (int i = 0; i <= 1000; i++) {
    C[i][0] = C[i][i] = 1;
    for (int j = 1; j < i; j++) {
        C[i][j] = (C[i - 1][j - 1] + C[i - 1][j]) % MOD;
    }
    }
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
    scanf("%d%d%d%d%d", &n, &m, &k, &x, &y);
    memset(dp1, 0, sizeof(dp1));
    memset(dp2, 0, sizeof(dp2));
    dp1[0][x] = dp2[0][y] = 1;
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= n; j++) {
        if (j >= 2)
            dp1[i][j] = (dp1[i][j] + dp1[i - 1][j - 2]) % MOD;
        if (j >= 1)
            dp1[i][j] = (dp1[i][j] + dp1[i - 1][j - 1]) % MOD;
        dp1[i][j] = (dp1[i][j] + dp1[i - 1][j + 1]) % MOD;
        dp1[i][j] = (dp1[i][j] + dp1[i - 1][j + 2]) % MOD;
        }
    }
    for (int i = 1; i <= k; i++) {
        for (int j = 1; j <= m; j++) {
        if (j >= 2)
            dp2[i][j] = (dp2[i][j] + dp2[i - 1][j - 2]) % MOD;
        if (j >= 1)
            dp2[i][j] = (dp2[i][j] + dp2[i - 1][j - 1]) % MOD;
        dp2[i][j] = (dp2[i][j] + dp2[i - 1][j + 1]) % MOD;
        dp2[i][j] = (dp2[i][j] + dp2[i - 1][j + 2]) % MOD;
        }
    }
    ll heng[N], shu[N];
    memset(heng, 0, sizeof(heng));
    memset(shu, 0, sizeof(shu));
    for (int i = 1; i <= n; i++)
        for (int kk = 0; kk <= k; kk++)
        heng[kk] = (heng[kk] + dp1[kk][i]) % MOD;
    for (int i = 1; i <= m; i++)
        for (int kk = 0; kk <= k; kk++)
        shu[kk] = (shu[kk] + dp2[kk][i]) % MOD;
    ll ans = 0;
    for (int i = 0; i <= k; i++) {
        ans = (ans + (heng[i] * shu[k - i] % MOD) * C[k][i] % MOD) % MOD;
    }
    printf("Case #%d:\n", ++cas);
    cout << ans << endl;
    }
    return 0;
}

HDU 4832(DP+计数问题),布布扣,bubuko.com

时间: 2024-10-06 13:28:10

HDU 4832(DP+计数问题)的相关文章

hdu 4832 dp ***

dp1[i][j]表示只走x轴走j步到i位置有多少总走法,dp2同,dp方程就很好写 wa了无数发,发现MOD写在INF上了 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cstring> 5 #include<cmath> 6 #include<queue> 7 #include<map> 8 using namespac

hdu 4832 Chess(dp)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 193    Accepted Submission(s): 59 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线

HDU 4832 Chess (DP)

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 24    Accepted Submission(s): 10 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字路线.

HDU 4832 Chess 排列组合 DP

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 351    Accepted Submission(s): 124 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘 上的走法遵循十字

HDU 4832 组合计数dp

Chess Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 509    Accepted Submission(s): 198 Problem Description 小度和小良最近又迷上了下棋.棋盘一共有N行M列,我们可以把左上角的格子定为(1,1),右下角的格子定为(N,M).在他们的规则中,"王"在棋盘上的走法遵循十字

HDU 4832 Chess

同样是百度之星的题目.刚开始看题目,觉得这是一道搜索的题,于是就萌生了找题解的想法.一开始就没有斗志,当然不会做出这道题的啦. 可是看完题解恍然大悟,原来是DP,而且很简单的一道DP.再一次失败,说明了看题解真的不是一个好习惯.我要改! 我要改!! 其实基本的思想就是把这个二维移动分开,变成一维的移动,最后加上组合数就OK了. 下面的是代码,虽然是自己敲的,但是还是剽窃过来的.... #include <iostream> #include <cstdio> #include &l

hdu 4832 百度之星初赛二B

把横的和竖的分开考虑 //#pragma comment(linker, "/STACK:102400000,102400000") #include<iostream> #include<vector> #include<algorithm> #include<cstdio> #include<queue> #include<stack> #include<string> #include<ma

hdu 3944 dp?

DP? Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 128000/128000 K (Java/Others)Total Submission(s): 1804    Accepted Submission(s): 595 Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0,1,2,-a

hdu 5389 dp类似背包

http://acm.hdu.edu.cn/showproblem.php?pid=5389 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft. Stilwell is enjoying the first chapter of this