HDU 6415 Rikka with Nash Equilibrium (DP)
要求在n*m矩陣中填入1-n*m,使得存在且僅存在1個位置,這個位置的數在所在的行與列都是最大值。問有多少種方案。
易知這個位置上必然是n*m。為了滿足有且僅有一個點符合條件,我們從n*m倒著往矩陣裡面放,顯然每個數都應該放在已經存在數的行或列上。
考慮用i個數已經在j行k列上放置了數字,在放第i+1個數之後只可能覆蓋j行k列、j+1行k列、j行k+1列。這樣分別有j*k-i、k*(n-j)、(m-k)*j種方法。依此進行狀態轉移,答案即為dp[n*m][n][m]。
第一次驚險的卡到了4882ms……後面優化到了3307ms
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 82;
const int INF = 0x3f3f3f3f;
int t, n, m;
ll mod, dp[maxn*maxn][maxn][maxn];
int main()
{
scanf("%d", &t);
while(t--)
{
scanf("%d%d%lld", &n, &m, &mod);
memset(dp, 0, sizeof(dp));
dp[1][1][1] = n*m;
for(int i = 1;i < n*m;i++)
{
for(int j = 1;j <= n;j++)
{
if(i < j) break;
for(int k = 1;k <= m;k++)
{
if(i < k) break;
if(!dp[i][j][k]) continue;
if(k < m)
dp[i+1][j][k+1] = (dp[i+1][j][k+1] + dp[i][j][k]*j*(m - k)) % mod;
if(j < n)
dp[i+1][j+1][k] = (dp[i+1][j+1][k] + dp[i][j][k]*(n - j)*k) % mod;
if(i < j*k)
dp[i+1][j][k] = (dp[i+1][j][k] + dp[i][j][k]*(j*k - i)) % mod;
}
}
}
printf("%lld\n", dp[n*m][n][m]);
}
return 0;
}
相關文章
- HDU6415:Rikka with Nash Equilibrium(dp)UI
- hdu 6415 Rikka with Nash EquilibriumUI
- HDU-6415 Rikka with Nash Equilibrium (DP/找規律)UI
- 【記憶優化搜尋/dp】HDU - 6415 - 杭電多校第九場 - Rikka with Nash Equilibrium優化UI
- [DP]HDU6415(2018多校訓練賽第九場 Problem A) Rikka with Nash Equilibrium 題解UI
- 【dp+組合數學】hdu 2018 多校第九場 1001 Rikka with Nash Equilibrium hdu 6415UI
- 2018 Multi-University Training Contest 9----hdu 6415 Rikka with Nash EquilibriumAIUI
- 【DFS】HDU 5423 Rikka with Tree
- HDU 5831 Rikka with Parenthesis II (括號匹配)
- HDU5425Rikka with Tree II(數學期望)
- HDU5424Rikka with Graph II(哈密頓圖判斷)
- HDU 3853 LOOPS(概率dp)OOP
- HDU 4669 Mutiples on a circle (DP , 統計)
- 【基礎dp】HDU 1260 Tickets
- hdu 3401 單調佇列+DP佇列
- HDU 4455 Substrings(預處理+dp)
- HDU 3853 LOOPS:期望dp【網格型】OOP
- 【dp】HDU - 1069 Monkey and BananaNaN
- hdu4374單調佇列+dp佇列
- HDU 5119 Happy Matt Friends(DP)APP
- HDU 5326 Work (基礎樹形dp)
- hdu 4123 樹形DP+RMQMQ
- 【基礎dp】HDU 1176 免費餡餅
- HDU 5067 Harry And Dig Machine(狀壓dp)Mac
- HDU5831(2016多校第八場)———Rikka with Parenthesis II(水題)
- HDU 1227 Fast Food(簡單二維dp)AST
- hdu 1069 Monkey and Banana(簡單dp)NaN
- HDU4427Math Magic (dp+滾動陣列)陣列
- HDU 3530 Subsequence (dp+單調佇列)佇列
- hdu 3507 斜率優化DP入門題優化
- hdu ---(4517)小小明系列故事——遊戲的煩惱(Dp)遊戲
- HDU3944 DP? (LUCAS定理+階乘預處理)
- 【基礎dp】HDU 1257 最少攔截系統
- hdu4313 貪心並查集 || 樹形dp並查集
- HDU 4326Game(比較難理解的概率dp)GAM
- hdu5435 數位dp(大數的處理)
- HDU 1028 Ignatius and the Princess III:dp or 母函式函式
- HDU-1466 計算直線的交點數 DP