HDU 6415(dp/找規律-2018多校第九場1001)

HarryBlackCat發表於2018-08-21

dp[i][j][k]表示的是放了i個棋子後佔了i行j列的情況,我們容易發現第一個棋子有n * m種情況可以放置,佔用位置是1行1列,而每加多一個棋子就會多增加1行或者1列,一直到所有的行列都佔用完了再把剩下的點放棋子,這樣我就可以從dp[i][j][k]推出dp[i + 1][j + 1][k]、dp[i + 1][j][k + 1]、dp[i + 1][j][k]三種狀態轉移方程做個dp加剪枝剛好卡過

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <list>
#define INF 0x3f3f3f3f
#define maxn 105000
#define maxnn 6000
#define juzheng 300
#define line cout << "-------------------------" << endl;
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define fill_(a,b,n) fill(a,a + n,b)
#define esp 1e-9

#define ri(n) scanf("%d",&n)
#define ri2(a,b) scanf("%d %d",&a,&b)
#define ri3(a,b,c) scanf("%d %d %d",&a,&b,&c)
#define rd(n) scanf("%lf",&n)
#define rd2(a,b) scanf("%lf %lf",&a,&b)
#define rd3(a,b,c) scanf("%lf %lf %lf",&a,&b,&c)
#define rl(n) scanf("%lld",&n)
#define rl2(a,b) scanf("%lld %lld",&a,&b)
#define rl3(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define rui(n) scanf("%u",&n)
#define rui2(a,b) scanf("%u %u",&a,&b)
#define rui3(a,b,c) scanf("%u %u %u",&a,&b,&c)
#define rs(str) scanf("%s",str)
#define pr(n) cout << n << endl
#define debug(str,x) cout << str << ":" << x << endl
#define ll long long
#define int64 __int64
#define ui unsigned int

using namespace std;

//const ll mod = 1e9 + 7;

//Date:2018-8-20
//Author:HarryBlackCat

ll dp[6403][81][81],n,m,mod;

void init() {
	for(ll i = 1;i <= n * m;i++){
		for(ll j = 1;j <= n;j++){
			for(ll k = 1;k <= m;k++){
				dp[i][j][k] = 0;
			}
		}
	}
	dp[1][1][1] = n * m;
}

int main() {
	//cin.sync_with_stdio(false);//降低cin,cout時間
	int t;
	while(~ri(t)) {
		while(t--) {
			rl3(n,m,mod);
			init();
			ll counter = 0;
			for(int i = 1; i < n * m; i++) {
				for(int j = 1; j <= n; j++) {
					for(int k = 1; k <= m; k++) {
						if(max(j,k) > i)
							break;
						if(!dp[i][j][k])
							continue;
						
						if(j < n) {
							dp[i + 1][j + 1][k] += dp[i][j][k] * k % mod * (n - j) % mod;
							dp[i + 1][j + 1][k] %= mod;
						}

						if(k < m) {
							dp[i + 1][j][k + 1] += dp[i][j][k] * j % mod * (m - k) % mod;
							dp[i + 1][j][k + 1] %= mod;
						}

						if(i < j * k) {
							dp[i + 1][j][k] += dp[i][j][k] * (j * k - i) % mod;
							dp[i + 1][j][k] %= mod;
						}

					}
				}
			}

			printf("%lld\n",dp[n * m][n][m] % mod);
		}
	}
	return 0;
}

 

相關文章