1. 程式人生 > >HDU多校9 Rikka with Nash Equilibrium(記憶化搜索/dp)

HDU多校9 Rikka with Nash Equilibrium(記憶化搜索/dp)

align previous msu higher ali mem import eno ont

Rikka with Nash Equilibrium

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 1460 Accepted Submission(s): 591


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,yAi,y i[1,n]Ax,yAx,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(1t20), the number of the testcases.

The first line of each testcase contains three numbers n,m and K(1n,m80,1K109).

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

Source 2018 Multi-University Training Contest 9

在一個矩陣中,如果僅有一個數同時是所在行所在列最大值,那麽這個數滿足納什均衡。

構造一個n*m的矩陣,裏面填入[1,n*m]互不相同的數字,求有多少種構造方案。

由題可得,滿足納什均衡的數一定是最大值n*m。因此我們可以從大往小依次將數填入矩陣,小數依附於大數的行或列,由此產生的三種行為:

1.所在列有大數,行+1 數+1

2.所在行有大數,列+1 數+1

3.所在行列都有大數,位於交界處,數+1

dp三種狀態[占有行數][占有列數][占有個數]進行求解。因為本題數據很強,所以需要以下優化:

1.盡可能得減少取模次數

2.註意狀態與遍歷順序保持一致

//記憶化搜索
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod,n,m;
ll dp[81][81][6401];
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+=y*(n-x)*dfs(x+1,y,z+1)%mod;
    if(y<m) tmp+=x*(m-y)*dfs(x,y+1,z+1)%mod;
    if(x*y>z) tmp+=(x*y-z)*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));  //答案有0的情況,因此初始化為-1
        scanf("%lld%lld%lld",&n,&m,&mod);
        dp[n][m][n*m]=1;
        ll ans=n*m*dfs(1,1,1)%mod;
        printf("%lld\n",ans);
    }
}
//dp
#include<bits/stdc++.h>
#define MAX 82
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;

ll dp[MAX][MAX][6402];    //註意順序

int main()
{
    int t,n,m,MOD,i,j,k;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d%d",&n,&m,&MOD);
        memset(dp,0,sizeof(dp));
        dp[1][1][1]=n*m;
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                for(k=1;k<n*m;k++){    //註意順序
                    dp[i+1][j][k+1]+=(n-i)*j*dp[i][j][k];
                    if(dp[i+1][j][k+1]>=MOD) dp[i+1][j][k+1]%=MOD;
                    dp[i][j+1][k+1]+=(m-j)*i*dp[i][j][k];
                    if(dp[i][j+1][k+1]>=MOD) dp[i][j+1][k+1]%=MOD;
                    if(i*j>k){
                        dp[i][j][k+1]+=(i*j-k)*dp[i][j][k];
                        if(dp[i][j][k+1]>=MOD) dp[i][j][k+1]%=MOD;
                    }
                }
            }
        }
        printf("%lld\n",dp[n][m][n*m]%MOD);
    }
    return 0;
}

HDU多校9 Rikka with Nash Equilibrium(記憶化搜索/dp)