Rikka with Nash Equilibrium
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
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.
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.
Sample Input
2
3 3 100
5 5 2333
Sample Output
64
1170
dp计算放置多少行多少列所可能得到的情况
增加新的点增加新的一行
增加新的点增加新的一列
增加新的点不增加行列:放在交点
#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll dp[6455][85][85]; //dp[i][j][k]:有i个点,占据了j行k列有几种放法
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m,mod;
scanf("%d%d%d",&n,&m,&mod);
memset(dp,0,sizeof(dp));
dp[1][1][1]=n*m;
for(int i=1;i<n*m;i++)
{
for(int j=1;j<=n;j++)
{
for(int k=1;k<=m;k++)
{
dp[i+1][j+1][k]=dp[i+1][j+1][k]+(n-j)*k*dp[i][j][k]; //占据的新的一行所能放的可能
dp[i+1][j][k+1]=dp[i+1][j][k+1]+(m-k)*j*dp[i][j][k]; //占据的新的一列所能放的可能
if(dp[i+1][j+1][k]>=mod) dp[i+1][j+1][k]%=mod; //mod太多会超时,微弱优化
if(dp[i+1][j][k+1]>=mod) dp[i+1][j+1][k]%=mod;
ll p=j*k-i; //计算有多少个可以放的交点
if(p<=0) continue; //不能放就跳过
dp[i+1][j][k]=dp[i+1][j][k]+p*dp[i][j][k]; //放交点不增加行列
if(dp[i+1][j][k]>=mod) dp[i+1][j][k]%=mod;
}
}
}
printf("%lld\n",dp[n*m][n][m]%mod);
}
return 0;
}