ACdream 1412 DP+排列组合
2-3 Trees
Problem Description
2-3 tree is an elegant data structure invented by John Hopcroft. It is designed to implement the same functionality as the binary search tree. 2-3 tree is an ordered rooted tree with the following properties:
- the root and each internal vertex have either 2 or 3 children;
- the distance from the root to any leaf of the tree is the same.
The only exception is the tree that contains exactly one vertex — in this case the root of the tree is the only vertex, and it is simultaneously a leaf, i.e. has no children. The main idea of the described properties is that the tree with l leaves has the height O(log l).
Given the number of leaves l there can be several valid 2-3 trees that have l leaves. For example, the picture below shows the two possible 2-3 trees with exactly 6 leaves.
Given l find the number of different 2-3 trees that have l leaves. Since this number can be quite large, output it modulo r.
Input
Output
Sample Input
6 1000000000 7 1000000000
Sample Output
2 3
Source
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> using namespace std; typedef long long LL; const int N=4e5+110,mods=20090717,inf=2e9+10; int n,mod,c[3001][3001]; LL dp[N]; int main() { while(scanf("%d%d",&n,&mod)!=EOF) { for(int i = 0; i <= 3000; ++i) { c[i][0] = 1; for(int j = 1; j <= i; ++j) { c[i][j] = (c[i-1][j] + c[i-1][j-1])%mod; } } memset(dp,0,sizeof(dp)); dp[1] = 1; for(int i = 1; i <= n; ++i) { for(int j = 0; j <= i; ++j) { if(j*2 + (i-j)*3 <= 5000) dp[j*2 + (i-j)*3] += dp[i]*c[i][j], dp[j*2 + (i-j)*3]%=mod; } } printf("%lld\n",dp[n]%mod); } return 0; }