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:
![](https://icpcarchive.ecs.baylor.edu:443/external/50/p5058a.png)
If the data is inserted with sequence: 2 1 3, the tree will be:
![](https://icpcarchive.ecs.baylor.edu:443/external/50/p5058b.png)
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:
![](https://icpcarchive.ecs.baylor.edu:443/external/50/p5058c.png)
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 ...
随机推荐
- MySQL 高可用:mysql+Lvs+Keepalived 负载均衡及故障转移
系统信息: mysql主库 mysql从库 VIP 192.168.1.150 mysql 主主同步都设置 auto-increment-offset,auto-increment-increment ...
- eclipse安装阿里代码扫描插件
1.首先打开eclipse软件,点击工具栏上的Help,选择Install New Soft进行安装新的插件. 2.进入插件安装界面,点击Add,弹出插件地址填写界面,也可以直接在市场上搜索关键字al ...
- 【转】WCF光芒下的Web Service
WCF光芒下的Web Service 学习.NET的开发人员,在WCF的光芒照耀下,Web Service 似乎快要被人遗忘了.因为身边做技术的人一开口就是WCF多么的牛逼!废话不多,本人很久不写博客 ...
- 2013 NEERC
2013 NEERC Problem A. ASCII Puzzle 题目描述:完成一个拼图. solution 暴搜,但好像挺难打的,但听说因为题目限制比较多,其实很多奇怪的情况都不存在. Prob ...
- 【hdu6334】【2018Multi-University-Training Contest04】Problem C. Problems on a Tree
维护1边的联通块和2边的联通块,合并的时候直接启发式合并. cdqz的大爷好强啊. #include<bits/stdc++.h> #define lson (o<<1) #d ...
- 【小程序开发】上拉加载更多demo
wxml: <scroll-view class='swiper-scroll' scroll-y="{{true}}" bindscrolltolower="lo ...
- thinkphp5 url传参
url('index/blog/read',['id'=>5,'name'=>'thinkphp']); 手册https://www.kancloud.cn/manual/thinkphp ...
- java容器---Comparable & Comparator
1.接口Comparable<T> API 参数类型:T ---可以与此对象进行比较的那些对象的类型 此接口强行对实现它的每个类的对象进行整体排序.这种排序被称为类的自然排序,类的c ...
- (三)HttpClient 抓取图片
第一节: HttpClient 抓取图片 这里pom.xml需要用到io输入输出: <dependency> <groupId>commons-io</groupId&g ...
- yum安装Mysql-5.6
MySQL yum库提供了一个简单的和方便的方法来安装和更新MySQL相关的软件包到最新版本. MySQL yum库文档说明:http://dev.mysql.com/doc/mysql-yum-re ...