Rikka with Nash Equilibrium
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 898 Accepted Submission(s): 346
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
Source
题意:
要求你构造一个N*M的矩阵,且矩阵中只含有一个(x,y)符合Nash Equilibrium的情况,即整个矩阵中,有一个数字保证在横行数列最大,其他数字在横行竖列中都有其他大于自身的数字。
法1:
公式法:n!*m!*(n+m)!/(n+m-1)!
解法:
然后(n+m)!/(n+m-1)!会爆longlong,要优化一下。
比如(2*3)/(2+3-1),可以化简为5*6;前面都是相同的。
1*2*3*4*5*6
1*2*3*4
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int _,n,m,k;
ll bit(ll n,ll m) {
if(n==0) {
return 1;
}
if(n==m) {
return n;
} else {
return (n * bit(n-1,m)%k);
}
}
int main() {
ll ans,a,b;
for(cin>>_; _; _--) {
scanf("%d %d %d",&n,&m,&k);
ans = bit(n,0)*bit(m,0)%k*bit(n*m,n+m)%k;
printf("%lld\n",ans);
}
}
法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) { //占领了x行,y列。已经放进去了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;
//printf("!!!%lld %lld %lld %lld %lld\n",dp[x][y][z],x,y,z,tmp);
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);
}
}