A - Rikka with Nash Equilibrium

题意:构造一个$n * m$的矩阵,使得$[1, n * m]$ 中每个数只出现一次,并且纳什均衡只出现一次。

思路:从大到小的放置,每一个都可以拓展一行拓展一列或者放在已经拓展的行列焦点,用记忆化搜索/dp即可

 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 typedef long long ll;
 6 
 7 int n, m;
 8 ll p;
 9 ll dp[81][81][81 * 81];
10 
11 ll DFS(int x, int y,int z)
12 {
13     if(z >= n * m) return 1;
14     if(dp[x][y][z] != -1) return dp[x][y][z];
15     ll res = 0;
16     if(x < n) res = (res + y * (n - x) % p * DFS(x + 1, y, z + 1)) % p;
17     if(y < m) res = (res + x * (m - y) % p * DFS(x, y + 1, z + 1)) % p;
18     if(x  * y > z) res = (res + (x * y - z) * DFS(x, y, z + 1)) % p;
19     dp[x][y][z] = res;
20     return res;
21 }
22 
23 int main()
24 {
25     int t;
26     scanf("%d", &t);
27     while(t--)
28     {
29         scanf("%d %d %lld", &n, &m, &p);
30         memset(dp, -1, sizeof dp);
31         ll ans = DFS(1, 1, 1);
32         ans = n * m % p * ans % p;
33         printf("%lld\n", ans);
34     }
35     return 0;
36 }
View Code

相关文章: