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(T100), 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 ...
随机推荐
- gcc 编译 + 选项【转】
转自:http://blog.csdn.net/princess9/article/details/6567678 一般来说要现有项目中的编译选项,设置新的project的编译选项 编译器 就是将“高 ...
- 我看到的最棒的Twisted入门教程!
http://www.douban.com/note/232204441/ http://www.cnblogs.com/sevenyuan/archive/2010/11/18/1880681.ht ...
- CEC2017 benchmark function调用接口
CEC2017 benchmark function可以从这里下载. 导师最近给了个课题让我自己研究,跟智能优化算法相关的,必不可免的要用到最新的CEC2017 benchmark function, ...
- mysql中间件 -> Atlas简介&安装
Atlas简介 Atlas是由 Qihoo 360公司Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上, ...
- 五、springcloud之客户端负载均衡Ribbon
一.简介 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的.Spring cloud有两种服务调用方式: 一种是ribbon+restTemplate, ...
- gitlab备份与还原
1.备份 登录原服务器,执行命令: gitlab-rake gitlab:backup:create 备份后文件在如下目录,下载该文件 /var/opt/gitlab/backups 2.还原 先安装 ...
- Oracle 函数 “申请通过后,将该表中循环遍历到的所有内容插到另一个表中”
create or replace function mcode_apply_insert_material(p_mca_no VARCHAR2, p_action VARCHAR2, p_wf_no ...
- IE手工导入证书
打开cer文件->欢迎使用证书导入向导->下一步->将所有的证书放入下列存储->受信任的根证书颁发机构->完成
- 浮动元素垂直居中,bootstrap栅格布局垂直居中
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU 1054 Strategic Game(最小路径覆盖)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1054 题目大意:给你一棵树,选取树上最少的节点使得可以覆盖整棵树. 解题思路: 首先树肯定是二分图,因 ...