dp tree count
Tree Count
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 29 Solved: 14
[Submit][Status][Web Board]
Description
Our superhero Carry Adder has uncovered the secret method that the evil Head Crash uses to
generate the entrance code to his fortress in Oakland. The code is always the number of distinct
binary search trees with some number of nodes, that have a specific property. To keep this code
short, he only uses the least significant nine digits.
The property is that, for each node, the height of the right subtree of that node is within one of
the height of the left subtree of that node. Here, the height of a subtree is the length of the longest
path from the root of that subtree to any leaf of that subtree. A subtree with a single node has a
height of 0, and by convention, a subtree containing no nodes is considered to have a height of −1.
Input
Input will be formatted as follows. Each test case will occur on its own line. Each line will contain
only a single integer, N, the number of nodes. The value of N will be between 1 and 1427, inclusive.
Output
Your output is one line per test case, containing only the nine-digit code (note that you must print
leading zeros).
Sample Input
Sample Output
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <iomanip>
using namespace std ;
const int MAXN = 1427 ;
const int LOGMAXN = 22 ; // log base phi of MAXN, or something in excess
long long a[MAXN+1][LOGMAXN+1] ; d[i][j]代表总的节点数是i,高度是j的平衡二叉树的个数
long long t[MAXN+1] ;
int main(int, char**) {
a[0][0] = a[1][1] =1 ;
t[0] = t[1] = 1 ;
int mind = 1 ;
for (int n=2; n<=MAXN; n++) {
long long alln = 0 ;
for (int d=mind; ; d++) {
long long s = 0 ;
int havesome = 0 ;
for (int i=0; i<n; i++) {
s += a[i][d-1] * a[n-1-i][d-1] ;
if (d > 1)
s += a[i][d-2] * a[n-1-i][d-1] +
a[i][d-1] * a[n-1-i][d-2] ;
if (s)
havesome = 1 ;
s %= 1000000000 ;
}
if (havesome == 0) {
if (d == mind)
mind++ ;
else
break ;
}
s %= 1000000000 ;
a[n][d] = s ;
alln += s ;
}
t[n] = alln % 1000000000 ;
}
int n ;
while (1) {
cin >> n ;
if (cin.fail())
break ;
cout << setfill('0') << setw(9) << t[n] << endl ;
}
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <iomanip>
using namespace std ;
const int MAXN = 1427 ;
const int LOGMAXN = 22 ; // log base phi of MAXN, or something in excess
long long a[MAXN+1][LOGMAXN+1] ; d[i][j]代表总的节点数是i,高度是j的平衡二叉树的个数
long long t[MAXN+1] ;
int main(int, char**) {
a[0][0] = a[1][1] =1 ;
t[0] = t[1] = 1 ;
int mind = 1 ;
for (int n=2; n<=MAXN; n++) {
long long alln = 0 ;
for (int d=mind; ; d++) {
long long s = 0 ;
int havesome = 0 ;
for (int i=0; i<n; i++) {
s += a[i][d-1] * a[n-1-i][d-1] ;
if (d > 1)
s += a[i][d-2] * a[n-1-i][d-1] +
a[i][d-1] * a[n-1-i][d-2] ;
if (s)
havesome = 1 ;
s %= 1000000000 ;
}
if (havesome == 0) {
if (d == mind)
mind++ ;
else
break ;
}
s %= 1000000000 ;
a[n][d] = s ;
alln += s ;
}
t[n] = alln % 1000000000 ;
}
int n ;
while (1) {
cin >> n ;
if (cin.fail())
break ;
cout << setfill('0') << setw(9) << t[n] << endl ;
}
}