PAT (Top Level) Practice1007 Red-black Tree (35 分)

题目链接:https://pintia.cn/problem-sets/994805148990160896/problems/994805153880719360

参考博客:https://blog.csdn.net/u014646950/article/details/48683081

算法:dp

#include
#include
#include
#include
#include
#include
const int mod = 1e9 + 7;
#define maxn 510
using namespace std;
long long dp[maxn][maxn][2];
int n;
long long sum = 0;
void Dp() {
	memset(dp, 0, sizeof(dp));
	dp[0][0][1] = dp[1][0][0] = 1;
	for (int i = 1; i < maxn; i++) {
		for (int j = 1; j <= min(i,8); j++) {
			for (int k = 0; k < i; k++) {
				(dp[i][j][1] += (dp[k][j - 1][1] + dp[k][j - 1][0])*(dp[i - k - 1][j - 1][1] + dp[i - k - 1][j - 1][0]) % mod) %= mod;
				(dp[i][j][0] += dp[k][j][1] * dp[i - k - 1][j][1] % mod)%=mod;
			}
			if (i == n) {
			    sum += dp[i][j][1];
				sum%=mod;
		    }
		}
		
	}
}
int main() {
	cin >> n;
	Dp();
	cout << sum << endl;
	return 0;
}

 

你可能感兴趣的:(算法)