标签:
那么计算方法是什么呢。
然后也有递推出来的解。设dp[n][2]。n表示还有n个子节点未分配。2表示0为最多分配n - 1个点,1为最多分配n个点,这样能保证子树都至少有两个节点。这样就是总情况了,直接用记忆化搜下去就可以
代码:
公式解:
#include <stdio.h> #include <string.h> int n; long long Catalan[30], SuperCatalan[30]; int main() { Catalan[1] = Catalan[2] = 1; for (int i = 3; i <= 26; i++) { Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i; } SuperCatalan[1] = SuperCatalan[2] = 1; for (int i = 3; i <= 26; i++) { SuperCatalan[i] = (3 * (2 * i - 3) * SuperCatalan[i - 1] - (i - 3) * SuperCatalan[i - 2]) / i; } while (~scanf("%d", &n)) { printf("%lld\n", SuperCatalan[n] - Catalan[n]); } return 0; }
#include <stdio.h> #include <string.h> int n; long long Catalan[30], dp[30][2]; long long dfs(int n, int flag) { long long &ans = dp[n][flag]; if (~ans) return ans; if (n <= 1) return ans = 1; ans = 0; for (int i = 1; i < n + flag; i++) ans += dfs(i, 0) * dfs(n - i, 1); return ans; } int main() { Catalan[1] = Catalan[2] = 1; for (int i = 3; i <= 26; i++) { Catalan[i] = Catalan[i - 1] * (4 * i - 6) / i; } while (~scanf("%d", &n)) { memset(dp, -1, sizeof(dp)); printf("%lld\n", dfs(n, 0) - Catalan[n]); } return 0; }
UVA 10312 - Expression Bracketing(数论+Catalan数)
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/5182150.html