http://acm.hdu.edu.cn/showproblem.php?pid=2292

题意:1-n个节点,题目给出了完全二叉树的定义(这个定义似乎有歧义,此题以题目描述为准),且要保持最小堆性质(根节点小于左右子树内的任意元素),问有多少种不同组合

解法:dp,dp[n]表示n个元素的合法排列数量。一共n个节点,左子树有a个节点,则右子树有n-1-a个节点,dp[n]=C(n-1,a)*dp[a]*dp[n-1-a],其中a可以轻易算出。

公式解释:除去根节点,在剩下的n-1个元素中取a个,这a个元素的合法排列有dp[a]种,剩下n-1-a个节点的合法排列有dp[n-1-a]种。

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std ;
typedef __int64 ll ;
ll c[][],n,m,dp[] ;
ll cal(int s)
{
ll temp=s- ;
ll cnt= ;
ll ans= ;
while(temp-cnt>)
{
ans+=cnt/ ;
temp-=cnt ;
cnt*= ;
}
if(temp>=cnt/)ans+=cnt/ ;
else ans+=temp ;
return ans ;
}
int main()
{
int t ;
scanf("%d",&t) ;
while(t--)
{
scanf("%I64d%I64d",&n,&m) ;
memset(c,,sizeof(c)) ;
for(int i= ;i< ;i++)
{
c[i][]=c[i][i]= ;
for(int j= ;j<i ;j++)
c[i][j]=(c[i-][j-]+c[i-][j])%m ;
}
memset(dp,,sizeof(dp)) ;
dp[]=dp[]= ;
for(int i= ;i<=n ;i++)
{
ll a=cal(i) ;
ll b=i--a ;
dp[i]=c[i-][a]*dp[a]%m*dp[b]%m ;
}
printf("%I64d\n",dp[n]%m) ;
}
return ;
}

HDU 2292的更多相关文章

  1. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  2. HDU 3572 Task Schedule(ISAP模板&amp;&amp;最大流问题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  , ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. MapReduce中的Shuffle和Sort分析

    MapReduce 是现今一个非常流行的分布式计算框架,它被设计用于并行计算海量数据.第一个提出该技术框架的是Google 公司,而Google 的灵感则来自于函数式编程语言,如LISP,Scheme ...

  2. 【Lua】面向对象编程(二)

      多重继承: module(...,package.seeall) local function search(k,plist) ,#plist do local v=plist[i][k] if ...

  3. stringbuffer 和 stringbuilder区别

    stringbuffer  和  stringbuilder速度                 小于         线程安全           线程非安全 单线程操作大量数据用stringbui ...

  4. ng-深度学习-课程笔记-14: 人脸识别和风格迁移(Week4)

    1 什么是人脸识别( what is face recognition ) 在相关文献中经常会提到人脸验证(verification)和人脸识别(recognition). verification就 ...

  5. 网关服务Spring Cloud Gateway(二)

    上一篇文章服务网关 Spring Cloud GateWay 初级篇,介绍了 Spring Cloud Gateway 的相关术语.技术原理,以及如何快速使用 Spring Cloud Gateway ...

  6. 关于GET POST

    先说相同点,只有了解了相同点之后才能理解为什么会发生混淆.两者都能向服务器发送数据,提交的“内容”[注1]的格式相同,都是var_1=value_1&var_2=value_2&... ...

  7. Window 常用系统变量

    转载:http://www.slyar.com/blog/envionment-variables.html 转载:http://blog.csdn.net/wuliusir/article/deta ...

  8. P4 PI库安装

    repo:Github PI P4Runtime - a control plane framework and tools for the P4 programming language 这个是P4 ...

  9. win10不能上网问题的解决办法

    升级到 Windows 10 以后,可以 ping 通外网,但是浏览器和各种客户端都不能正常访问网络了.百度以后得到如下解决办法: 以管理员身份运行cmd,输入netsh winsock reset后 ...

  10. pycharm同时使用python2.7版本和python3.6版本

    最近在看爬虫的专题,很多爬虫的教程是python2的,电脑上装的是3.6版本,而且python不向下兼容,这就很麻烦,最简单的print要加括号啊,等等.于是分享一个在windows环境下pychar ...