Problem Description
Nash Equilibrium is an important concept i 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.
Input
The 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.
Output
For each testcase, output a single line with a single number: the answer modulo K.
问题描述:
要你构造一个n * m 的矩阵:这个矩阵里面由数字1 到 n * m构成,要求这个矩阵里只有一个数是同时是这一行、这一列是最大的(显然这个数是这个矩阵里面最大的数也就是n *m,且n*m-1这个数必须与n*m处在同一行或同一列,n*m-2这个数必须与n*m-1或n*m-2处在同一行或同一列,这样就构成了题目所要求的矩阵)。问你有多少种方法来构造这个矩阵。
综上所述,我们可以从大到小一个个放数字进去,每放进去一个数字,它的这一行这一列就被占领(这一行这一列就可以放数字了)。可以发现每放进去一个数字会有三种状态:
1、多占领一行;
2、多占领一列;
3、没有多占领。即处在原先占领行列的交叉口。
然后就可以记忆优化搜索了,只要写的不难看是可以刚好卡过的。如果写成dp的话会更快。
注意记忆优化的数组初始值不能赋值为零,因为答案也有可能是零。
有一个没什么用的优化,如果所有的地方都已经占领了,那么剩下的可能数就是一个阶乘。不会变快很多,因为剩下的数字相对来说会很小。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod,n,m;
const ll N=6405;
ll dp[85][85][85*85];
ll dfs(ll x,ll y,ll z){
if(dp[x][y][z]!=-1) return dp[x][y][z];
ll tmp=0;
if(x<n) tmp=(tmp+y*(n-x)%mod*dfs(x+1,y,z+1))%mod;
if(y<m) tmp=(tmp+x*(m-y)%mod*dfs(x,y+1,z+1))%mod;
if(x*y>z) tmp=(tmp+(x*y-z)%mod*dfs(x,y,z+1))%mod;
return dp[x][y][z]=tmp;
}
int main(){
ll t;
scanf("%lld",&t);
while(t--){
memset(dp,-1,sizeof(dp));
scanf("%lld%lld%lld",&n,&m,&mod);
dp[n][m][n*m]=1;
ll ans=m*n%mod*dfs(1,1,1)%mod;
printf("%lld\n",ans);
}
}