UVALive 5058 Counting BST 数学

B - Counting BST

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Binary Search Tree (BST) is a rooted binary tree data structure which has following properties:

  • Left subtree contains only nodes with value less than the node's value.
  • Right subtree contains only nodes with value greater than the node's value.
  • All values in the nodes are unique.
  • Both left and right subtrees are also binary search tree recursively.

If there is a new node to be inserted, the following algorithm will be used:

  1. If the root is empty, then the new node becomes the root and quit, else continue to step 2.
  2. Set the root as current node.
  3. If the new node's value is less than current node's value:
    • If current node's left is empty, then set the new node as current node's left-child and quit.
    • else set current node's left-child as current node, and repeat step 3.
  4. If the new node's value is greater than current node's value:
    • If current node's right is empty, then set the new node as current node's right-child and quit.
    • else set current node's right-child as current node, and repeat step 3.

BST structure depends on its data inserting sequence. Different
sequence may yield a different structure though the data set is the
same. For example:

Insert sequence: 1 2 3, the BST will be:

UVALive 5058 Counting BST 数学

If the data is inserted with sequence: 2 1 3, the tree will be:

UVALive 5058 Counting BST 数学

On the other hand, different data set may have a same BST structure.

For example: Insert sequence 2 1 3 will have the same BST structure with 4 6 2, and the tree will be:

UVALive 5058 Counting BST 数学

Given N
nodes BST, calculate how many distinct insert data sequence which
result in the same BST structure, assuming that data are taken from
range 1..M.

Input

The first line of input contains an integer T(TUVALive 5058 Counting BST 数学100), the number of test cases. Each case begins with two integers N and M(1UVALive 5058 Counting BST 数学NUVALive 5058 Counting BST 数学MUVALive 5058 Counting BST 数学1, 000), the number of nodes in BST and the maximum range respectively. The next line contains N integers Ai(1UVALive 5058 Counting BST 数学AiUVALive 5058 Counting BST 数学1, 000) the insert sequence that construct a BST.

Output

For each case, output an integer denoting the number of distinct insert
data sequence which result in the same BST structure, assuming that
data are taken from range 1..M. ç Modulo this number with 1,000,003.

Note: Explanation for the 1st sample input.

There are 8 insert sequences (data taken from 1..4) which have the same BST:

  1. 2 1 3
  2. 2 3 1
  3. 2 1 4
  4. 2 4 1
  5. 3 1 4
  6. 3 4 1
  7. 3 2 4
  8. 3 4 2

Sample Input

3
3 4
3 1 4
3 5
1 2 3
4 4
2 1 10 3

Sample Output

8
10
3
#include<bits/stdc++.h>
#define eps 1e-9
#define FOR(i,j,k) for(int i=j;i<=k;i++)
#define MAXN 1005
#define MAXM 40005
#define MOD 1000003
#define INF 0x3fffffff
using namespace std;
typedef long long LL;
LL i,j,k,n,m,x,y,T,ans,big,cas,w,t,u,v;
bool flag;
LL a[],num[];
LL yh[][]; void BuildYangHui(LL n)
{
LL i,j;
yh[][]=;yh[][]=;
for (i=;i<=n;i++)
{
yh[i][]=;
for (j=;j<=n;j++)
{
yh[i][j]=(yh[i-][j-]+yh[i-][j])%MOD;
}
}
} LL lc[],rc[];
void BuildBST(LL n)
{
LL cur=;
for (LL i=;i<=n;i++)
{
cur=;
while ()
{
if (a[i]<a[cur])
{
if (!lc[cur])
{
lc[cur]=i;
break;
}else
cur=lc[cur];
}else
{
if (!rc[cur])
{
rc[cur]=i;
break;
}else
cur=rc[cur];
}
}
}
} LL CalcNodes(LL u)//以u为根的子树的节点数
{
if (u==) return ;
if (num[u]!=) return num[u];
return num[u]=CalcNodes(lc[u])+CalcNodes(rc[u])+;
} LL FUNC(LL u)
{
if (u==) return ; return yh[ num[lc[u]]+num[rc[u]] ][ num[rc[u]] ]* FUNC(lc[u]) % MOD *FUNC(rc[u]) %MOD;
} int main()
{
scanf("%lld",&T);
BuildYangHui();
while (T--)
{
scanf("%lld%lld",&n,&m);
for (i=;i<=n;i++)
{
scanf("%lld",&a[i]);
}
memset(num,,sizeof(num));
memset(lc,,sizeof(lc));
memset(rc,,sizeof(rc));
BuildBST(n);//构造BST树
CalcNodes();//计算结点数
printf("%lld\n",FUNC()*yh[m][n]%MOD);
}
return ;
}
上一篇:史上最强php生成pdf文件,html转pdf文件方法


下一篇:构图和意境是一张摄影作品的关键