【BZOJ2111】[ZJOI2010]Perm 排列计数

Description

称一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2<=i<=N时,Pi>Pi/2. 计算1,2,...N的排列中有多少是Magic的,答案可能很大,只能输出模P以后的值

Input

输入文件的第一行包含两个整数 n和p,含义如上所述。

Output

输出文件中仅包含一个整数,表示计算1,2,⋯,的排列中, Magic排列的个数模 p的值。

Sample Input

20 23

Sample Output

16

HINT

100%的数据中,1 ≤ N ≤ 106, P ≤ 10^9,p是一个质数。

题解:题意可转化为:求n个节点能构成的完全二叉堆的个数。显然我们可以求出左右两棵子树的大小,然后分别递归下去即可。

细节有点多~

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1000010;
int m=1000000;
ll n,p;
ll jc[maxn],jcc[maxn],ine[maxn],f[maxn];
int Log[maxn];
ll C(ll a,ll b)
{
if(a<b) return 0;
if(!b) return 1;
if(a<p&&b<p) return jc[a]*jcc[b]%p*jcc[a-b]%p;
return C(a%p,b%p)*C(a/p,b/p)%p;
}
ll calc(ll x)
{
if(f[x]) return f[x];
ll a=x-(1<<Log[x+1])+1;
if(a<(1<<Log[x+1]-1)) a=(1<<Log[x+1]-1)-1+a;
else a=(1<<Log[x+1])-1;
return f[x]=C(x-1,a)*calc(a)%p*calc(x-a-1)%p;
}
int main()
{
scanf("%lld%lld",&n,&p);
if(m>=p) m=p-1;
ll i;
jc[0]=jcc[0]=1,ine[0]=ine[1]=1;
for(i=2;i<=m;i++) ine[i]=(p-(p/i)*ine[p%i]%p)%p;
for(i=1;i<=m;i++) jc[i]=jc[i-1]*i%p,jcc[i]=jcc[i-1]*ine[i]%p;
for(i=2;i<=n+1;i++) Log[i]=Log[i>>1]+1;
f[0]=f[1]=1;
printf("%lld",calc(n));
return 0;
}

【BZOJ2111】[ZJOI2010]Perm 排列计数 组合数的更多相关文章

  1. BZOJ2111: [ZJOI2010]Perm 排列计数

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2111 题意:一个1,2,...,N的排列P1,P2...,Pn是Magic的,当且仅当2< ...

  2. [BZOJ2111][ZJOI2010]Perm排列计数(组合数学)

    题意就是求一个n个点的堆的合法形态数. 显然,给定堆中所有数的集合,则这个堆的根是确定的,而由于堆是完全二叉树,所以每个点左右子树的大小也是确定的. 设以i为根的堆的形态数为F(i),所以F(i)+= ...

  3. [bzoj2111][ZJOI2010]Perm 排列计数 ——问题转换,建立数学模型

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

  4. [BZOJ2111]:[ZJOI2010]Perm 排列计数(组合数学)

    题目传送门 题目描述 称一个1,2,...,N的排列${P}_{1}$,${P}_{2}$,...,${P}_{N}$是Magic的,当且仅当2≤i≤N时,${P}_{i}$>${P}_{\fr ...

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

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

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

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

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

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

  8. 【bzoj2111】[ZJOI2010]Perm 排列计数 dp+Lucas定理

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

  9. 【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是素数 ...

随机推荐

  1. SharePoint 2013 Custom MasterPage

    <%@Master language="C#"%> <%@ Register Tagprefix="SharePoint" Namespace ...

  2. [LeetCode] Sort Colors 只有3个类型的排序

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  3. WebRTC源码架构浅析(转)

    Google 在2010年花了6千8百万美元收购了大名鼎鼎的 Global IP Sound/Solutions (GIPS) 公司, 得到了它的 VoIP 相关技术的专利和软件. 第二年, Goog ...

  4. bq25896 charging status CHRG_STAT register 0xB

    condition 1 :                    adapter 全部電流往 system去,                    battery current 也往 system ...

  5. 回调函数 typedef bool (*IsUsed)(const string &name,boost::shared_ptr<ShpGeometry> oneGeometry);

    就是指向函数的指针. 回调函数,表示了一个函数的地址,将函数作为参数进行使用.参考百度百科:http://baike.baidu.com/view/414773.htm 常用的大概就是在sort函数中 ...

  6. LeetCode OJ——Validate Binary Search Tree

    http://oj.leetcode.com/problems/validate-binary-search-tree/ 判断一棵树是否为二叉搜索树.key 是,在左子树往下搜索的时候,要判断是不是子 ...

  7. JS-日历签到

    实现的功能: 首先这是前端显示的内容,没有后台的配置哈: 1.显示当前年月下的日历表: 2.今天的日期独有背景色: 3.当月今天之前的日子号数颜色变浅,表示日期已过: 4.点击日期签到:(只能点击当天 ...

  8. LightOj 1215 Finding LCM

    Discription LCM is an abbreviation used for Least Common Multiple in Mathematics. We say LCM (a, b, ...

  9. 0.从零开始搭建spring mvc + mybatis + memcached+ dubbo\zookper的maven项目

    1.首先创建maven 项目,配置相关pom信息 2.配置spring mvc 4, 测试,提交代码 3.引入配置mybatis3,测试,提交代码 4.配置事务,测试,提交代码 5.配置memcach ...

  10. XSY1036 [Apio2012]派遣

    题面 Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个 ...