UVALive 5058 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:
- If the root is empty, then the new node becomes the root and quit, else continue to step 2.
- Set the root as current node.
- 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.
- 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(T
100), the number of test cases. Each case begins with two integers N and M(1
N
M
1, 000), the number of nodes in BST and the maximum range respectively. The next line contains N integers Ai(1
Ai
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:
- 2 1 3
- 2 3 1
- 2 1 4
- 2 4 1
- 3 1 4
- 3 4 1
- 3 2 4
- 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 数学的更多相关文章
- UVALive 5058 Counting BST --组合数
题意:排序二叉树按照数插入的顺序不同会出现不同的结构,现在要在1~m选n个数,使按顺序插入形成的结构与给出的结构相同,有多少种选法. 解法:先将给出的结构插入,构造出一棵排序二叉树,再dfs统计,首先 ...
- acdream.A Very Easy Triangle Counting Game(数学推导)
A - A Very Easy Triangle Counting Game Time Limit:1000MS Memory Limit:64000KB 64bit IO Forma ...
- 1148 - Mad Counting(数学)
1148 - Mad Counting PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...
- lightoj 1148 Mad Counting(数学水题)
lightoj 1148 Mad Counting 链接:http://lightoj.com/volume_showproblem.php?problem=1148 题意:民意调查,每一名公民都有盟 ...
- LightOJ - 1148-Mad Counting (数学)
链接: https://vjudge.net/problem/LightOJ-1148 题意: Mob was hijacked by the mayor of the Town "Trut ...
- UVaLive 6602 Counting Lattice Squares (找规律)
题意:给定一个n*m的矩阵,问你里面有几面积为奇数的正方形. 析:首先能知道的是,大的矩阵是包括小的矩阵的,而且面积为奇数,我们只要考虑恰好在边界上的正方形即可,画几个看看就知道了,如果是3*3的有3 ...
- UVa 1640 The Counting Problem (数学,区间计数)
题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...
- UVALive 6602 Counting Lattice Squares
给定一个n*m的网格,求面积为奇数的正方形有多少个. 首先是n*m个面积为1的,然后剩下的要么是边长为奇数,要么被这样一个奇数边长所包围. 原因如下: 对于一个边长不平行于坐标抽的正方形,其边长一定是 ...
- CF1101D GCD Counting(数学,树的直径)
几个月的坑终于补了…… 题目链接:CF原网 洛谷 题目大意:一棵 $n$ 个点的树,每个点有点权 $a_i$.一条路径的长度定义为该路径经过的点数.一条路径的权值定义为该路径经过所有点的点权的 GC ...
随机推荐
- c语言格式控制符
http://zhidao.baidu.com/link?url=-YJjz3U0fd_eSW9eLa8ankGo_QbyOOOaKYWyAY9g4mKWQj0DN6l12OSLJz24U8jCwo1 ...
- @RequestParam,@PathParam,@PathVariable,@QueryParam注解的使用区别
获取url模板上数据的(/{id})@DefaultValue 获取请求参数的(包括post表单提交)键值对(?param1=10¶m2=20).可以设置defaultValue JA ...
- 安装pywin32模块
1.先下载pywin32对于的版本 下载地址:python for windows extensions 2.选择自己对应的版本,我的是python3.5版本 注意注意注意:此处一定要看清楚自己的py ...
- 记一次对 Laravel-permission 项目的性能优化
我最近研究分析了在 SWIS上面创建的项目的性能.令人惊讶的是,最耗费性能的方法之一是优秀的 spatie/laravel-permission 包造成的. 经过查阅更多资料和研究,发现一个可能明显 ...
- Robust Mesh Watermarking
之前看了一篇题为"Robust Mesh Watermarking"的论文,查阅资料的时候发现了一篇与之很相似的名为"三维模型数字水印系统的设计与实现"的中文论 ...
- python类型学习
python类型学习 标准类型 数字 Integer 整型 Boolean 布尔型 Long integer 长整型 Floating point real numer 浮点型 Complex nu ...
- node模拟socket
什么是Socket?网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. Socket通信流程 基于net模块实现socket 服务端SocketServer.j ...
- spring-boot分环境打包为jar包
1.pom配置 <!-- 多环境打包 start --> <profiles> <!-- 开发环境配置 --> <profile> <id> ...
- window服务器上搭建git服务,window server git!!!
先给大家看一个高大上的,这是我给我公司配置的,小伙伴们都说好! 阿里云的2012server 基于这篇大神的教程,我把服务端搭建好了. 传送门,当然我还是自己做个笔记的好. 1.下载java,并安装 ...
- win10字体大小设置
有时图形化界面不能正常显示,需要改变字体大小来查看 更改字体大小: 更改字体: