hdu-6415 计数DP

Nash Equilibrium is an important concept in game theory.

Rikka and Yuta are playing a simple matrix game. At the beginning of the game, Rikka shows an n×m integer matrix A. And then Yuta needs to choose an integer in [1,n], Rikka needs to choose an integer in [1,m]. Let i be Yuta‘s number and j be Rikka‘s number, the final score of the game is Ai,j.

In the remaining part of this statement, we use (i,j) to denote the strategy of Yuta and Rikka.

For example, when n=m=3 and matrix A is

???111241131???

If the strategy is (1,2), the score will be 2; if the strategy is (2,2), the score will be 4.

A pure strategy Nash equilibrium of this game is a strategy (x,y) which satisfies neither Rikka nor Yuta can make the score higher by changing his(her) strategy unilaterally. Formally, (x,y) is a Nash equilibrium if and only if:

{Ax,y≥Ai,y  ∀i∈[1,n]Ax,y≥Ax,j  ∀j∈[1,m]

In the previous example, there are two pure strategy Nash equilibriums: (3,1) and (2,2).

To make the game more interesting, Rikka wants to construct a matrix A for this game which satisfies the following conditions:

1. Each integer in [1,nm] occurs exactly once in A.

2. The game has at most one pure strategy Nash equilibriums.

Now, Rikka wants you to count the number of matrixes with size n×m

which satisfy the conditions.

InputThe first line contains a single integer t(1≤t≤20), the number of the testcases.

The first line of each testcase contains three numbers n,m and K(1≤n,m≤80,1≤K≤109).

The input guarantees that there are at most 3 testcases with max(n,m)>50.OutputFor each testcase, output a single line with a single number: the answer modulo K.Sample Input

2
3 3 100
5 5 2333

Sample Output

64
1170

OJ-ID:
hdu-6415

author:
Caution_X

date of submission:
20191026

tags:
dp

description modelling:
给定N×M的矩阵A,若该矩阵满足{A(x,y)≥A(i,y)  ?i∈[1,n],A(x,y)≥A(x,j)  ?j∈[1,m]}且该矩阵元素分别是1~N*M,则称这是矩阵A的一个方案。输入N,M,K,输出N×M矩阵的方案数模K的值。

major steps to solve it:
从最大的数开始依次选择一个位置来存放,第一个数有N×M种放置方法,后面每一个数都必须保证和之前放过的数同一行或者同一列。
首先第一个数会产生一个新行和一个新列,第二个数会产生一个新行或者一个新列,第三个数同第二个数,第四个数及第四个数之后的所有数都分三种情况讨论:
(1)新加入之后产生了一个新行
(2)新加入之后产生了一个新列
(3)既没有产生新行也没有产生新列
设dp[i][j][k],表示已经产生了i个行,j个列,用掉了k个数
那么则:
(1)dp[i][j][k]+=dp[i][j][k-1]*(i*j-i+1)%MOD,没有产生新行或者新列
(2)dp[i][j][k]+=dp[i-1][j][k-1]*(n-i+1)%MOD,产生了新行
(3)dp[i][j][k]+=dp[i][j-1][k-1]*(m-j+1)%MOD,产生了新列

AC code:

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long int LL;
LL T, n, m, mod, dp[88][88][2], p;
int main()
{
    scanf("%lld", &T);
    while(T--) {
        scanf("%lld%lld%lld", &n, &m, &mod);
        memset(dp, 0, sizeof dp); p = 0;
        dp[1][1][1] = n * m % mod;
        for(int i=2;i<=n*m;i++,p^=1) {
            for(int j=1;j<=n;j++) {
                for(int k=1;k<=m;k++) {
                    dp[j][k][p] = 0;
                    if(j * k < i) continue;
                    dp[j][k][p] = dp[j][k][p^1] * (j*k-i+1) % mod;
                    dp[j][k][p] = (dp[j][k][p] + dp[j-1][k][p^1] * (n-j+1) * k) % mod;
                    dp[j][k][p] = (dp[j][k][p] + dp[j][k-1][p^1] * (m-k+1) * j) % mod;
                }
            }
        }
        printf("%lld\n", dp[n][m][p^1]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cautx/p/11746184.html

时间: 2024-10-19 02:02:17

hdu-6415 计数DP的相关文章

hdu 4901 The Romantic Hero(计数dp)2014多校训练第4场1005

The Romantic Hero                                                                               Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Problem Description There is an old country and the king fell in lov

hdu 5136 Yue Fei&#39;s Battle(计数DP)

Yue Fei's Battle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 151    Accepted Submission(s): 48 Problem Description Yue Fei is one of the most famous military general in Chinese history.He

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 6377 度度熊看球赛 (计数DP)

度度熊看球赛 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 293    Accepted Submission(s): 130 Problem Description 世界杯正如火如荼地开展!度度熊来到了一家酒吧. 有 N 对情侣相约一起看世界杯,荧幕前正好有 2×N 个横排的位置. 所有人都会随机坐在某个位置上. 当然,如果某一对情

HDU4815/计数DP

题目链接[http://acm.hdu.edu.cn/showproblem.php?pid=4815] 简单说一下题意: 有n道题,每到题答对得分为a[ i ],假如A不输给B的最小概率是P,那么A最少要得到多少分. 解题过程: 假设有n道题,每个题有两个状态,胜或者败,假设达到某个分数m有k(计数DP)种方式,那么最后是这个分数的概率是k/pow(2,n).那么A不输给B的概率就是小于等于m的分数的概率和. #include<bits/stdc++.h> const int maxn =

HDU4901 The Romantic Hero 计数DP

2014多校4的1005 题目:http://acm.hdu.edu.cn/showproblem.php?pid=4901 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 393    Accepted Submission(s): 150 Problem Description There i

hdu 2151 Worm (DP)

Worm Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2637    Accepted Submission(s): 1707 Problem Description 自从见识了平安夜苹果的涨价后,Lele就在他家门口水平种了一排苹果树,共有N棵.突然Lele发现在左起第P棵树上(从1开始计数)有一条毛毛虫.为了看到毛毛虫变蝴蝶的过程

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 3853 概率DP 简单

http://acm.hdu.edu.cn/showproblem.php?pid=3853 题意:有R*C个格子,一个家伙要从(0,0)走到(R-1,C-1) 每次只有三次方向,分别是不动,向下,向右,告诉你这三个方向的概率,以及每走一步需要耗费两个能量,问你走到终点所需要耗费能量的数学期望: 回头再推次,思想跟以前的做过的类似 注意点:分母为0的处理 #include <cstdio> #include <cstring> #include <algorithm>

HDU 4968 (水dp 其他?)

1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <map> 6 using namespace std; 7 const int inf = 0x3f3f3f3f; 8 const int MAX = 200+10; 9 double GPA[10],dp1[20][30000],dp2[20][30000