是我想复杂了

首先发现大于关系构成了一棵二叉树的结构,于是树形dp

设f[i]为i点的方案数,si[i]为i点的子树大小,递推式是\( f[i]=f[i*2]*f[i*2+1]*C_{si[i]-1}^{si[i*2]} \)

组合数用Lucas求

#include<iostream>
#include<cstdio>
using namespace std;
long long n,p,f[5000005],jc[5000005],s[5000005];
long long ksm(long long a,long long b)
{
long long r=1;
while(b)
{
if(b&1)
r=r*a%p;
a=a*a%p;
b>>=1;
}
return r;
}
long long C(long long x,long long y)
{
if(y>x)
return 0;
return jc[x]*ksm(jc[y]*jc[x-y]%p,p-2)%p;
}
long long luc(long long x,long long y)
{
if(!y)
return 1;
return C(x%p,y%p)*luc(x/p,y/p)%p;
}
void dfs(long long u)
{
s[u]=1;f[u]=1;f[u<<1]=1;f[u<<1|1]=1;
if((u<<1)<=n)
dfs(u<<1),s[u]+=s[u<<1];
if((u<<1|1)<=n)
dfs(u<<1|1),s[u]+=s[u<<1|1];
f[u]=f[u<<1]*f[u<<1|1]%p*luc(s[u]-1,s[u<<1])%p;
}
int main()
{
scanf("%lld%lld",&n,&p);
jc[0]=jc[1]=1;
for(long long i=2;i<=n;i++)
jc[i]=jc[i-1]*i%p;
dfs(1);
printf("%lld",f[1]);
return 0;
}

bzoj 2111: [ZJOI2010]Perm 排列计数【树形dp+lucas】的更多相关文章

  1. bzoj 2111 [ZJOI2010]Perm 排列计数(DP+lucas定理)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2111 [题意] 给定n,问1..n的排列中有多少个可以构成小根堆. [思路] 设f[i ...

  2. bzoj 2111: [ZJOI2010]Perm 排列计数 (dp+卢卡斯定理)

    bzoj 2111: [ZJOI2010]Perm 排列计数 1 ≤ N ≤ 10^6, P≤ 10^9 题意:求1~N的排列有多少种小根堆 1: #include<cstdio> 2: ...

  3. BZOJ 2111: [ZJOI2010]Perm 排列计数 [Lucas定理]

    2111: [ZJOI2010]Perm 排列计数 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 1936  Solved: 477[Submit][ ...

  4. BZOJ 2111 [ZJOI2010]Perm 排列计数:Tree dp + Lucas定理

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2111 题意: 给定n,p,问你有多少个1到n的排列P,对于任意整数i∈[2,n]满足P[i ...

  5. bzoj 2111: [ZJOI2010]Perm 排列计数 Lucas

    题意:称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很大, ...

  6. bzoj 2111: [ZJOI2010]Perm 排列计数

    神题... 扒自某神犇题解: http://blog.csdn.net/aarongzk/article/details/50655471 #include<bits/stdc++.h> ...

  7. 2111: [ZJOI2010]Perm 排列计数

    2111: [ZJOI2010]Perm 排列计数 链接 题意: 称一个1,2,...,N的排列$P_1,P_2...,P_n$是Magic的,当且仅当$2<=i<=N$时,$P_i> ...

  8. 【BZOJ】2111: [ZJOI2010]Perm 排列计数 计数DP+排列组合+lucas

    [题目]BZOJ 2111 [题意]求有多少1~n的排列,满足\(A_i>A_{\frac{i}{2}}\),输出对p取模的结果.\(n \leq 10^6,p \leq 10^9\),p是素数 ...

  9. 【BZOJ2111】[ZJOI2010]Perm 排列计数 组合数

    [BZOJ2111][ZJOI2010]Perm 排列计数 Description 称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi> ...

随机推荐

  1. bit manipulation

    WIKI Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorte ...

  2. Codeforces 549C The Game Of Parity【博弈】

    C语言纠错大赛的一道题,正好拿来补博弈~~ 给的代码写的略奇葩..不过还是直接在上面改了.. 题目链接: http://codeforces.com/problemset/problem/549/C ...

  3. Delphi字符串加密/解密

    unit uEncrypt_Decrypt;   interface   uses SysUtils;   const XorKey: array[0..7] of Byte = ($B2, $09, ...

  4. IDEA添加作者注释

    1.打开IDEA的settings,然后在Editor下找到File and Code Templates 2.然后选择File Header 选择需要注释的的格式即可.

  5. Unity从实践中学习(1)

    首先在实际中unity的开发之中快捷键应该是相当重要的一个部分,这里先引用csdn的一个博客,https://blog.csdn.net/qq_34552886/article/details/697 ...

  6. vs npm设置淘宝npm

    VS2017自带的npm会去国外的镜像下载文件, 奇慢无比, 还是马云家淘宝的镜像适合国内用户. 淘宝npm镜像地址:  https://registry.npm.taobao.org VS中使用淘宝 ...

  7. struts2 input file多文件同时通过ajax提交

    <input type="file" name="files" multiple="multiple">必须是multiple才 ...

  8. 原来,表名和字段名不能在pdo中“参数化查询”

    https://stackoverflow.com/questions/182287/can-php-pdo-statements-accept-the-table-or-column-name-as ...

  9. Android Activity与远程Service的通信学习总结

    当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...

  10. Object.getOwnPropertyNames()

    1.Object.getOwnPropertyNames(),遍历实例属性(包括不可枚举),返回属性名组成的数组 var arr = ["a", "b", &q ...