BZOJ 2111: [ZJOI2010]Perm 排列计数(简单组合数学)
2013-01-02 20:29
465 查看
2111: [ZJOI2010]Perm 排列计数
组合计数
求1-n排列中对于任意2<=i<=N 有 Pi>P(i/2) 的排列个数 mod 质数 p 的余数;
问题等价于求1~n组成一棵满足小根堆性质的完全二叉树的方案数;
定义f[i]为当这棵完全二叉树有i个节点时的方案数;
则 f[i] = C(i-1,left) * f[left] * f[i-1-left];
f[0] = f[1] = 1;
下面来求left
深度 - 1 = floor(log2(i));
记 tot = 前深度 - 1 层总结点数 = pow(2,深度-1)-1;
则 left = (tot-1)/2 + min(i-tot,(tot+1)/2);
下面来求C(n,k) mod p
直接 预处理 n! mod p , 费马小定理求乘法逆元;
组合计数
求1-n排列中对于任意2<=i<=N 有 Pi>P(i/2) 的排列个数 mod 质数 p 的余数;
问题等价于求1~n组成一棵满足小根堆性质的完全二叉树的方案数;
定义f[i]为当这棵完全二叉树有i个节点时的方案数;
则 f[i] = C(i-1,left) * f[left] * f[i-1-left];
f[0] = f[1] = 1;
下面来求left
深度 - 1 = floor(log2(i));
记 tot = 前深度 - 1 层总结点数 = pow(2,深度-1)-1;
则 left = (tot-1)/2 + min(i-tot,(tot+1)/2);
下面来求C(n,k) mod p
直接 预处理 n! mod p , 费马小定理求乘法逆元;
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> using namespace std; int fac[1000010],f[1000010]; int n,p; inline int pow(int x,int y,int p) { int res(1); x %= p; while(y) { if(y&1) res = (long long)res * x % p; x = (long long)x * x % p; y >>= 1; } return res; } inline int C(int n,int m,int p) { return (long long)fac *pow((long long)fac[m]*fac[n-m]%p,p-2,p) % p; } inline int solve(int x) { if(f[x]) return f[x]; int depth = log2(x); int tot = pow(2,depth,10000000)-1; int left = (tot-1>>1) + min(x - tot,tot+1>>1); return f[x] = (long long)C(x-1,left,p)*solve(left)%p*solve(x-left-1)%p; } int main() { scanf("%d%d",&n,&p); fac[0] = 1; for(int i = 1; i <= n; i++) fac[i] = (long long)fac[i-1] * i % p; f[0] = f[1] = 1; printf("%d\n",solve(n)); return 0; }
相关文章推荐
- [BZOJ2111][ZJOI2010]Perm 排列计数(组合数学+lucas定理)
- [BZOJ2111][ZJOI2010]Perm排列计数(组合数学)
- [bzoj2111][ZJOI2010]Perm 排列计数(组合数学)
- BZOJ 2111 ZJOI2010 Perm 排列计数 组合数学+Lucas定理
- BZOJ 2111: [ZJOI2010]Perm 排列计数|组合数学|Lucas定理|DP
- bzoj 2111: [ZJOI2010]Perm 排列计数 (组合数学+Lucas定理)
- BZOJ 2111 ZJOI2010 Perm 排列计数 组合数学+Lucas定理
- [bzoj2111][ZJOI2010]Perm 排列计数 ——问题转换,建立数学模型
- [数学杂题]BZOJ 2111—— [ZJOI2010]Perm 排列计数
- [BZOJ2111][ZJOI2010]Perm 排列计数 && 数学
- bzoj2111 [ZJOI2010]Perm 排列计数
- BZOJ 2111 [ZJOI2010]Perm 排列计数
- Bzoj2111:[ZJOI2010]Perm 排列计数:树形动态规划+组合数学
- bzoj2111 Perm 排列计数 组合数学
- BZOJ2111 [ZJOI2010]Perm 排列计数
- bzoj 2111 [ZJOI2010]Perm 排列计数(DP+lucas定理)
- 【bzoj2111】[ZJOI2010]Perm 排列计数 dp+Lucas定理
- 【BZOJ 2111】 [ZJOI2010]Perm 排列计数
- 【递推+lucas定理】BZOJ2111 [ZJOI2010]Perm 排列计数
- BZOJ 2111([ZJOI2010]Perm 排列计数-乘法逆元+完全二叉树模型+数列分数表示法)