hdu 6415Rikka with Nash Equilibrium(dp/输入挂)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37025443/article/details/81909747

Rikka with Nash Equilibrium

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


题目链接

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

 题意:

给你一个n,m,k,让你构造一个n*m的矩阵(里面的元素是1-n*m),满足一下条件——整个矩阵有且仅有一个元素同时是该行和该列的最大值

问你这样的矩阵有多少个,答案%K

解析:

这种题目完全被迷惑了...以为是数学题,结果是DP,果然还是太菜了

dp[i][j][k]表示当前已经选了i个元素,这i个元素占据了j行,k列

这个选元素一定是从大到小顺序选的,第一个一定是n*m
这样其实就很好写转移方程了

dp[i][j][k]+=dp[i-1][j][k]*(j-1-(i-1))

表示当前再选一个元素,不会占据新的行或列

dp[i][j][k]+=dp[i-1][j-1][k]*(n-j+1)*k

表示选新的一行,然后再在该行与已选列的交点中选择一个作为新的元素的位置

dp[i][j][k]+=dp[i-1][j][k-1]*(m-k+1)*j

表示选新的一列,然后再在该列与已选行的交点中选择一个作为新的元素的位置

这题虽然开了5s,理论上O(n^4)是不会T的,但是好像因为有t组,所以会卡常....

后来发现卡常的是dp里面的取模运算里,因为数据的值不大,所以只需要加在最后加一个%就可以了

我一开始每一个转移方程都加了4个%,就T了,然后每删掉一个%,就快一秒,从5s,慢慢变成2s

当然这里剪枝也是可以的,加一些条件判断就可了,我一开始没找到常数的原因时,剪枝就过了,不过刚刚卡过,4.4s

#include <cstdio>
#include <cstring>
#include <algorithm>
#define  Min(a,b) (a<=b?a:b)
using namespace std;
typedef long long ll;
const int MAXN = 88;
int dp[MAXN*MAXN][MAXN][MAXN];

namespace fastIO {

#define BUF_SIZE 1000000

    //fread -> read

    bool IOerror = 0;

    inline char nc() {

        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;

        if(p1 == pend) {

            p1 = buf;

            pend = buf + fread(buf, 1, BUF_SIZE, stdin);

            if(pend == p1) {

                IOerror = 1;

                return -1;

            }

        }

        return *p1++;

    }

    inline bool blank(char ch) {

        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';

    }

    inline void read(int &x) {

        char ch;

        while(blank(ch = nc()));

        if(IOerror)

            return;

        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');

    }

#undef BUF_SIZE

};

using namespace fastIO;

int main()
{
    int t;
    read(t);
    while(t--)
    {
        int n,m;
        int K;
		read(n);
		read(m);
		read(K);
        //scanf("%d%d%d",&n,&m,&K);
        dp[1][1][1]=n*m;
        for(int i=2;i<=n*m;i++)
        {
            for(int j=1;j<=(Min(i,n));j++)
            {
                for(int w=1;w<=(Min(i,m));w++)
                {
                    if(j*w<i) continue;
                    dp[i][j][w]=0;
                    if(j*w>i-1)
                        dp[i][j][w]=(dp[i][j][w]+1ll*dp[i-1][j][w]*(j*w-(i-1)))%K;
                    dp[i][j][w]=(dp[i][j][w]+1ll*dp[i-1][j-1][w]*(n-j+1)*w)%K;
                    dp[i][j][w]=(dp[i][j][w]+1ll*dp[i-1][j][w-1]*(m-w+1)*j)%K;
                }
            }
        }
        printf("%d\n",dp[n*m][n][m]);
    }
    return 0;
}

这里我特地看了一下jls的代码,想学习一下,发现jlsdp的第一维只开了2!dp[2][][],因为每次状态转移只需要前后两个状态就好了,那么就在这2维倒来倒去就可以了

#include <cstdio>
#include <cstring>
#include <algorithm>
#define  Min(a,b) (a<=b?a:b)
using namespace std;
typedef long long ll;
const int MAXN = 88;
int dp[2][MAXN][MAXN];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,m;
        int K;
        scanf("%d%d%d",&n,&m,&K);
        memset(dp[0],0x00,sizeof(dp[0]));
        dp[0][1][1]=n*m;
        int now=0;
        //int tot=1;
        for(int i=1;i<n*m;i++)
        {
            int ne=now^1;
            memset(dp[ne],0x00,sizeof(dp[ne]));
            for(int j=1;j<=n;j++)
            {
                for(int w=1;w<=m;w++)
                {
                    if(dp[now][j][w])
                    {
                        if(j*w-i>0)
                            dp[ne][j][w]=(dp[ne][j][w]+1ll*dp[now][j][w]*(j*w-i))%K;
                        dp[ne][j+1][w]=(dp[ne][j+1][w]+1ll*dp[now][j][w]*(n-j)*w)%K;
                        dp[ne][j][w+1]=(dp[ne][j][w+1]+1ll*dp[now][j][w]*(m-w)*j)%K;
                    }
                }
            }
            now=ne;
        }
        printf("%d\n",dp[now][n][m]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37025443/article/details/81909747
今日推荐