hdu 6415 Rikka with Nash Equilibrium

YVVVVY發表於2018-08-21

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);
    }
}

 

相關文章