link:
https://codeforces.com/contest/1228/problem/E
Meaning of the questions:
You have n×n square grid and an integer k. Put an integer in each cell while satisfying the conditions below.
All numbers in the grid should be between 1 and k inclusive.
Minimum number of the i-th row is 1 (1≤i≤n).
Minimum number of the j-th column is 1 (1≤j≤n).
Find the number of ways to put integers in the grid. Since the answer can be very large, find the answer modulo (109+7).
These are the examples of valid and invalid grid when n=k=2.
Ideas:
DP [i] [j] has a front row i and column j is 1 while ensuring that each line 1, consider the transfer, when the transfer is equal to a number of the upper and lower ranks.
, Considered alone, column 1 may be any value, but there must be a guarantee of the current line 1 by 1.
Code:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1e9+7;
LL C[300][300];
LL Dp[300][300];
LL M1[300], M2[300];
LL n, k;
int main()
{
C[0][0] = C[1][0] = C[1][1] = 1;
for (int i = 2;i <= 250;i++)
{
C[i][0] = C[i][i] = 1;
for (int j = 1;j < i;j++)
C[i][j] = (C[i-1][j]+C[i-1][j-1])%MOD;
}
M1[0] = M2[0] = 1;
cin >> n >> k;
for (int i = 1;i <= n;i++)
M1[i] = (M1[i-1]*k)%MOD, M2[i] = (M2[i-1]*(k-1))%MOD;
//k^i
for (int i = 1;i <= n;i++)
Dp[1][i] = (C[n][i]*M2[n-i])%MOD;
for (int i = 2;i <= n;i++)
{
for (int j = 1;j <= n;j++)
{
for (int p = j;p <= n;p++)
{
LL res = ((C[n-j][p-j]*M2[n-p])%MOD*M1[j])%MOD;
if (p == j)
res = ((M1[j]-M2[j])*M2[n-j])%MOD;
LL sum = (Dp[i-1][j]*res)%MOD;
Dp[i][p] = (Dp[i][p]%MOD + sum + MOD)%MOD;
}
}
}
cout << Dp[n][n] << endl;
return 0;
}