The Number of the Same BST_组合数优化_2018_2_21
2018-02-21 16:09
549 查看
Many people knows binary search tree. The keys in a binary search tree are always stored in such a way as to satisfy the BST property:
Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] <= key[x]. If y is a node in the right subtree of x, then key[y] > key[x].
For example,
It is a binary search tree. And it can be built by inserting the elements of vector A <12, 6, 3, 18, 20, 10, 4, 17, 20> sequentially. But it can also be built by the vector B <12, 18, 17, 6, 20, 3, 10, 4, 20>.
Now given a vector X, then you may get a binary search tree from X. Your job is to calculate how many different vectors can build the same binary search tree. To make it easy, you should just output the number of different vectors mod 9901.
Input Input consists of several cases. Each case starts with a line containing one positive integer n, which is the length of test vector. The integer n is less than 100. Following this there will be n positive integers, which are less then 10000, on the next line. The input will end with a case starting with n = 0. This case should not be processed. Output For each test case, print a line with a single integer, which is the number of different vectors mod 9901. Sample Input
Let x be a node in a binary search tree. If y is a node in the left subtree of x, then key[y] <= key[x]. If y is a node in the right subtree of x, then key[y] > key[x].
For example,
It is a binary search tree. And it can be built by inserting the elements of vector A <12, 6, 3, 18, 20, 10, 4, 17, 20> sequentially. But it can also be built by the vector B <12, 18, 17, 6, 20, 3, 10, 4, 20>.
Now given a vector X, then you may get a binary search tree from X. Your job is to calculate how many different vectors can build the same binary search tree. To make it easy, you should just output the number of different vectors mod 9901.
Input Input consists of several cases. Each case starts with a line containing one positive integer n, which is the length of test vector. The integer n is less than 100. Following this there will be n positive integers, which are less then 10000, on the next line. The input will end with a case starting with n = 0. This case should not be processed. Output For each test case, print a line with a single integer, which is the number of different vectors mod 9901. Sample Input
3 2 1 3 9 5 6 3 18 20 10 4 17 20 0Sample Output
2 168
#include<iostream> #include<cstring> using namespace std; const int mod=9901; const int N=3e5+10; int a ,size ,n,ans; void insert(int x){ int p=1; while(size[p]){ size[p]++; if(x<=a[p])p*=2; else p=p*2+1; } a[p]=x; size[p]=1; } int cal(int x){ int s=x,i; for(i=1;i<=mod;i++){ if(s%mod==1)break; s+=x; } return i; } void dfs(int p){ ans=ans*cal(size[p])%mod; if(size[p*2])dfs(p*2); if(size[p*2+1])dfs(p*2+1); } int main(){ while(scanf("%d",&n),n){ memset(a,0,sizeof(a)); memset(size,0,sizeof(size)); int i,x; for(int i=1;i<=n;i++){ scanf("%d",&x); if(i==1)a[i]=x,size[i]=1; else insert(x); } ans=1; dfs(1); for(int i=2;i<=n;i++) ans=ans*i%mod; printf("%d\n",ans); } }
相关文章推荐
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- The number of Oracle redo threads (2) is not the same as the number of checkpoint threads (1)
- python中关于Np.hstack()错误的ValueError: all the input arrays must have same number of dimensions
- Nhibernate: Foreign key must have same number of columns as the referenced primary key
- 题目描述 Given n points on a 2D plane, find the maximum number of points that lie on the same straight
- Leetcode--->Given n points on a 2D plane, find the maximum number of points that lie on the same str
- [LeetCode] Next Permutation - Next bigger number with the same set of digits
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
- hdu 4727 The Number Off of FFF 简单题( 2013 ACM/ICPC Asia Regional Online —— Warmup2 1012)
- the number of position
- matlab:Warning: Unable to determine the number of frames in this file.
- HDU 4727 The Number Off of FFF
- sdut 2623 The number of steps (概率dp)
- svn问题之一: Failed to add file 'xxx': an unversioned file of the same name already exists
- UPC2225: The number of steps
- Reducing the Number of Gray Levels,Zoming and Shrinking Images
- Error:The number of method references in a .dex file cannot exceed 64K.
- perfect squares find the least number of perfect square numbers (1, 4, 9, 16, ...) which sum to n
- 3006 The Number of set