Zjoi2010排列计数Perm
这东西还是挺有思想的,道听途说一些东西,问问DuanYue同志,然后自己打表画树推了推,就搞出来了。
首先根据p i>p i/2(向下取整)这种形式,如果线段树学的好的人,一定能看出来,这是在唯一标号法标号后的形式,即父亲的权值大于两个儿子的权值,这是一个小根堆的样子。
那问题就是求给定数目的数字,求其能构成小根堆的个数。
这是一个类似树形dp的问题,我们设f[i]表示以当前节点为根所能构成小根堆的个数,那么有状态转移方程:
就是说当前节点及其子树一共分配了size[i]个数字,然后分给了左右子树,有上式Combine种可能,然后把左右儿子的贡献转移上来。
组合数还是老规矩打阶乘及其逆元的表,用lucas定理求一下,因为不用的话模数小的时候会崩,模数大了lucas也可以自动求组合取模。
实现过程中没有定义dp数组(其实size数组也没必要),直接带返回值的dfs搞一下。
代码略丑,勿看(主要是懒,直接define int long long 了)。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#define int long long
using namespace std;
int n,p;
int size[];
int fac[],inv[];
int qpow(int x,int k){
int ans=;
for(;k;k>>=,x=1ll*x*x%p)
if(k&) ans=1ll*ans*x%p;
return ans;
}
void pre(){
fac[]=;
for(int i=;i<min(n,p);i++)
fac[i]=1ll*fac[i-]*i%p;
inv[min(n,p)-]=qpow(fac[min(n,p)-],p-);
for(int i=min(n,p)-;i>=;i--)
inv[i-]=1ll*inv[i]*i%p;
}
int com(int x,int y){
if(y>x) return ;
return ((1ll*fac[x]*inv[y])%p*inv[x-y]%p)%p;
}
int lucas(int x,int y){
if(!y) return ;
return com(x%p,y%p)*lucas(x/p,y/p)%p;
}
int dfs(int x){
if(x>n) return ;
size[x]++;
int a=dfs(x<<)%p;
int b=dfs(x<<|)%p;
size[x]+=size[x<<]+size[x<<|];
return 1ll*((a*b)%p*(lucas(size[x]-,size[x<<])%p))%p;
}
signed main(){
scanf("%lld%lld",&n,&p);
pre();
/*for(int i=1;i<=p;i++)
cout<<fac[i]<<" ";cout<<endl;
for(int i=1;i<=p;i++)
cout<<inv[i]<<" ";cout<<endl;*/
printf("%lld",dfs()%p);
return ;
}
Zjoi2010排列计数Perm的更多相关文章
- 【BZOJ2111】[ZJOI2010]排列计数(组合数学)
[BZOJ2111][ZJOI2010]排列计数(组合数学) 题面 BZOJ 洛谷 题解 就是今年九省联考\(D1T2\)的弱化版? 直接递归组合数算就好了. 注意一下模数可以小于\(n\),所以要存 ...
- [ZJOI2010]排列计数 (组合计数/dp)
[ZJOI2010]排列计数 题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有 ...
- 洛谷 P2606 [ZJOI2010]排列计数 解题报告
P2606 [ZJOI2010]排列计数 题目描述 称一个\(1,2,...,N\)的排列\(P_1,P_2...,P_n\)是\(Magic\)的,当且仅当对所以的\(2<=i<=N\) ...
- P2606 [ZJOI2010]排列计数
P2606 [ZJOI2010]排列计数 因为每个结点至多有一个前驱,所以我们可以发现这是一个二叉树.现在我们要求的就是以1为根的二叉树中,有多少种情况,满足小根堆的性质. 设\(f(i)\)表示以\ ...
- bzoj2111 [ZJOI2010]排列计数
Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic ...
- 洛谷P2606 [ZJOI2010]排列计数(组合数 dp)
题意 题目链接 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案 ...
- [ZJOI2010]排列计数
题目描述 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很 ...
- ●洛谷P2606 [ZJOI2010]排列计数
题链: https://www.luogu.org/problemnew/show/P2606题解: 组合数(DP),Lucas定理 首先应该容易看出,这个排列其实是一个小顶堆. 然后我们可以考虑dp ...
- BZOJ2111:[ZJOI2010]排列计数——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2111 https://www.luogu.org/problemnew/show/P2606#su ...
随机推荐
- c#调用带用户名密码验证的wsdl
之前记录过一篇添加带验证的webservice,但是公司的另一个项目是.net framework2.0的项目,没有服务引用,只能添加web引用. 现在记录和分享一下方法: 先添加web引用,选择ws ...
- API接口利用ActionFilterAttribute实现接口耗时检测
1.主要代码 using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; ...
- windows服务总结
一.创建windows服务项目创建完成后结构,如: 其中,Program.cs代码: using System; using System.Collections.Generic; using Sys ...
- 【vue】computed 和 watch
计算属性:computed 监听多个变量且变量是在vue实例中(依赖某个变量,变量发生改变就会触发) 侦听器: watch 监听一个变量的变化 使用场景:watch(异步场景) ...
- input 禁止输入特殊字符
方式一:拿到value值以后 在你传递之前处理 function stripscript(value) { var pattern = new RegExp("[`~!@#$^&*( ...
- CSS—BFC原理解析与应用
我们在很多地方都见过BFC这个词,或许能够知道大概意思,但是有时候它的具体原理以及作用会记得很模糊,下面就对BFC这个概念深入学习下. 块级格式化上下文(Block Formatting Contex ...
- 3.Hibernate基础配置
1.Hibernate.cfg.xml:hbm2ddl.auto 在SessionFactory创建时,自动检查数据库结构,或者将数据库schema的DDL导出到数据库 <property na ...
- In Unix, what is tar, and how do I use it?
In Unix, the name of the tar command is short for tape archiving, the storing of entire file syste ...
- How to: Compile Linux kernel 2.6
Compiling custom kernel has its own advantages and disadvantages. However, new Linux user / admin ...
- pandas中Series对象下的str所拥有的方法(df["xx"].str)
在使用pandas的时候,经常要对DataFrame的某一列进行操作,一般都会使用df["xx"].str下的方法,但是都有哪些方法呢?我们下面来罗列并演示一下.既然是df[&qu ...