[2_3_nocows] DP=>Search+Memorial; Solution: Boundary=Whole-Inside
来源:互联网 发布:网易域名管理平台 编辑:程序博客网 时间:2024/05/05 12:44
Cow Pedigrees
Farmer John is considering purchasing a new herd of cows. In this new herd, each mother cow gives birth to two children. The relationships among the cows can easily be represented by one or more binary trees with a total of N (3 <= N < 200) nodes. The trees have these properties:
- The degree of each node is 0 or 2. The degree is the count of the node's immediate children.
- The height of the tree is equal to K (1 < K <100). The height is the number of nodes on the longest path from the root to any leaf; a leaf is a node with no children.
How many different possible pedigree structures are there? A pedigree is different if its tree structure differs from that of another pedigree. Output the remainder when the total number of different possible pedigrees is divided by 9901.
PROGRAM NAME: nocows
INPUT FORMAT
- Line 1: Two space-separated integers, N and K.
SAMPLE INPUT (file nocows.in)
5 3
OUTPUT FORMAT
- Line 1: One single integer number representing the number of possible pedigrees MODULO 9901.
SAMPLE OUTPUT (file nocows.out)
2
OUTPUT DETAILS
Two possible pedigrees have 5 nodes and height equal to 3:@ @ / \ / \ @ @ and @ @ / \ / \ @ @ @ @
This is a DP problem. The properties of a tree that we are interested in are depth and number of nodes, so we'll make a table: table[i][j] contains the number of trees with depth i and number of nodes j. Given the constraints of the task, j must be odd. How do you construct a tree? From smaller trees, of course. A tree of depth i and j nodes will be constructed from two smaller trees and one more node.
With i and j already chosen, we chose k, which is the number of nodes in the left subtree. Then the number of nodes in the right subtree is known, j-k-1. For depth, at least one subtree has to have depth i-1 so that the new made tree would have depth i. There are three possibilities: the left subtree can have depth i-1 and the depth of the right subtree can be smaller, the right subtree can have depth i-1 and the depth of the left subtree can be smaller, or they can both have depth i-1.
The truth is that once we are constructing trees of depth i, we use smaller trees, but we only care if those are of depth i-1 or smaller. So, let another array, smalltrees[i-2][j] contain number of trees of any depth smaller than i-1, not just i-2. Now, knowing all this, we contruct our tree from three possible ways:
table[i][j] += smalltrees[i-2][k]*table[i-1][j-1-k]; // left subtree smaller than i-1, right is i-1table[i][j] += table[i-1][k]*smalltrees[i-2][j-1-k]; // left subtree is i-1, right smallertable[i][j] += table[i-1][k]*table[i-1][j-1-k]; // both i-1
In addition, if the number of nodes in the left subtree is smaller then the number of nodes in the left subtree, we can count the tree twice, as different tree can be constructed by swapping left and right subtree.
Total running time is O(K*N^2),with very favorable constant factor.
#include <cstdio>#include <cstdlib>#include <cassert>#define MOD 9901using namespace std;int table[101][202],N,K,c;int smalltrees[101][202];FILE *fin=fopen("nocows.in","r");FILE *fout=fopen("nocows.out","w");int main() { fscanf (fin,"%d %d",&N,&K); table[1][1]=1; for (int i=2;i<=K;i++) { for (int j=1;j<=N;j+=2) for (int k=1;k<=j-1-k;k+=2) { if (k!=j-1-k) c=2; else c=1; table[i][j]+=c*( smalltrees[i-2][k]*table[i-1][j-1-k] // left subtree smaller than i-1 +table[i-1][k]*smalltrees[i-2][j-1-k] // right smaller +table[i-1][k]*table[i-1][j-1-k]);// both i-1 table[i][j]%=MOD; } for (int k=0;k<=N;k++) { // we ensure that smalltrees[i-2][j] inthe next i smalltrees[i-1][k]+=table[i-1][k]+smalltrees[i-2][k]; // iterationcontains the number smalltrees[i-1][k]%=MOD; // of trees smaller than i-1 and withj nodes } } fprintf (fout,"%d\n",table[K][N]); return 0;}
my (better) solution, dp[i][j] state is # of diff trees with i nodes and its height <= j
#include <cstdio>#include <cmath>#include <cstring>const int MOD=9901;int dp[200][100];int calc(int num,int height){if(dp[num][height]!=-1)return dp[num][height];if((num&1)==0 || log(double(num))/log(2.0)>=height)return (dp[num][height]=0);int ans=0;for(int i=1;i<=num-2;i+=2)ans=(ans+calc(i,height-1)*calc(num-1-i,height-1))%MOD;return (dp[num][height]=ans);}int main(){freopen("nocows.in","r",stdin);freopen("nocows.out","w",stdout);int n,k;scanf("%d%d",&n,&k);memset(dp,-1,sizeof(dp));for(int i=1;i<=k;i++)dp[1][i]=1;int ans=(calc(n,k)-calc(n,k-1))%MOD;printf("%d\n",ans>=0?ans:ans+MOD);return 0;}
- [2_3_nocows] DP=>Search+Memorial; Solution: Boundary=Whole-Inside
- [LeetCode] Search a 2D Matrix, Solution
- Activity selection Problem (DP solution 2)
- boundary
- [DP] 576Out of boundary path
- LeetCode 74 Search a 2D Matrix with java solution
- Lintcode28 Search a 2D Matrix solution 题解
- Lintcode38 Search a 2D Matrix II solution 题解
- Lintcode123 Word Search solution 题解
- Lintcode132 Word Search || solution 题解
- Google+BenQ=Google Inside
- New - low - high boundary; kind of binary search
- DP O(n)遍历 + Binary Search = O(nlogn)
- allowDefinition='MachineToApplication' error solution
- leetcode distinct subsequences dp solution
- leetcode combination sum dp solution
- Solution of ZOJ 2840 File Search
- [LeetCode 35] Search Insert Position Solution
- poj1328
- dojo AMD规范
- Subversion安装配置教程
- 连接数据库之独孤九剑
- 第十六天(C++代码重用)
- [2_3_nocows] DP=>Search+Memorial; Solution: Boundary=Whole-Inside
- 正则表达式
- ARM 开发板 与 PC机ubuntu系统 nfs 挂载
- Quoit Design
- Android下增加service和对应的AIDL
- 将工作环境迁移到 Linux
- ORACLE 常用日期函数
- 冒泡排序
- 在eclipse中误创建了超长的文件夹而引发的“无法删除指定的文件名无效或太长”问题