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:

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

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:

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(T100), the number of test cases. Each case begins with two integers N and M(1NM1, 000), the number of nodes in BST and the maximum range respectively. The next line contains N integers Ai(1Ai1, 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 ;
}

UVALive 5058 Counting BST 数学的更多相关文章

  1. UVALive 5058 Counting BST --组合数

    题意:排序二叉树按照数插入的顺序不同会出现不同的结构,现在要在1~m选n个数,使按顺序插入形成的结构与给出的结构相同,有多少种选法. 解法:先将给出的结构插入,构造出一棵排序二叉树,再dfs统计,首先 ...

  2. acdream.A Very Easy Triangle Counting Game(数学推导)

    A - A Very Easy Triangle Counting Game Time Limit:1000MS     Memory Limit:64000KB     64bit IO Forma ...

  3. 1148 - Mad Counting(数学)

    1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...

  4. lightoj 1148 Mad Counting(数学水题)

    lightoj 1148 Mad Counting 链接:http://lightoj.com/volume_showproblem.php?problem=1148 题意:民意调查,每一名公民都有盟 ...

  5. LightOJ - 1148-Mad Counting (数学)

    链接: https://vjudge.net/problem/LightOJ-1148 题意: Mob was hijacked by the mayor of the Town "Trut ...

  6. UVaLive 6602 Counting Lattice Squares (找规律)

    题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...

  7. UVa 1640 The Counting Problem (数学,区间计数)

    题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...

  8. UVALive 6602 Counting Lattice Squares

    给定一个n*m的网格,求面积为奇数的正方形有多少个. 首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围. 原因如下: 对于一个边长不平行于坐标抽的正方形,其边长一定是 ...

  9. CF1101D GCD Counting(数学,树的直径)

    几个月的坑终于补了…… 题目链接:CF原网  洛谷 题目大意:一棵 $n$ 个点的树,每个点有点权 $a_i$.一条路径的长度定义为该路径经过的点数.一条路径的权值定义为该路径经过所有点的点权的 GC ...

随机推荐

  1. 『实践』百度地图给多个marker添加右键菜单(删除、更新)

    js: $.getJSON("./GetStationPlaceServlet",function(json){ for(var i=0;i<json.length;i++) ...

  2. 使用postman做接口测试(一)

    参考大神的总结:https://www.cnblogs.com/Skyyj/p/6856728.html 一,先了解一下基础知识,虽然工作中没什么卵用,但背会了,可以显摆自己很专业的样子,以下内容来自 ...

  3. Python开发环境(1):Eclipse+PyDev插件

    电脑:小米笔记本电脑Pro 15.6寸(i5-8250U),操作系统:Windows 10,JDK版本:1.8.0_152(环境变量已配置) Step 1.下载Eclipse 根据我的CPU型号,选择 ...

  4. docker centos:last 开启sshd 遇到的证书问题

    启动sshd: # /usr/sbin/sshd 一.问题描述 这时报以下错误: [root@ xxx/]# /usr/sbin/sshd Could not load host key: /etc/ ...

  5. SQL2000数据库修改sa密码

    开始——程序——Microsoft SQL Server——企业管理器 2 展开数据库Microsoft SQL Server—— SQL Server组——安全性——登录——双击sa 3 在常规内有 ...

  6. CVE-2010-3974 Microsoft Windows多个平台Fax Cover Page Editor内存破坏漏洞

    Microsoft Windows Fax Cover Pages用于个性化传真以及呈现更正式外观的传真传输.         Microsoft Windows XP SP2和SP3,Windows ...

  7. iOS图片缓存

    iOS的内存管理始终是开发者面临的大问题,内存占用过大时,很容易会被系统kill掉,开发者需要尽可能的优化内存占用问题. 现在的App界面做的越来越精致,里面集成了大量的图片,笔者首先想到的就是如何减 ...

  8. CCF CSP 201703-5 引水入城(50分)

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201703-5 引水入城 问题描述 MF城建立在一片高原上.由于城市唯一的水源是位于河谷地带的 ...

  9. Python类和人类

    Python中的类 俗话说,物以类聚,人以群分,类是什么,类就是一组相同属性的集合.下面来结合人,探讨Python中类和人类的关系. 首先,我们定义一个人的类,如下: class People(obj ...

  10. 支持多个title,解决主副标题分别对齐