标签:const 大小 ret printf ons include || geo 处理
序列大小关系构成树形小根堆关系
设f[i]表示大小为i的堆由多少种形态
那么f[n] = f[l] * f[r] * C(n - 1,l),l,r为左右子数大小
对于每个n左子树的大小是一定的,可以处理出
组合数取膜要lucas
如果是求字典序最小就变成九省联考题了2333
#include<cstdio>
#include<algorithm>
inline int read() {
int x = 0,f = 1;
char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c <= '9' && c >= '0') x = x * 10 + c - '0',c = getchar();
return x * f;
}
#define int long long
#define LL long long
const int maxn = 1000007;
int f[maxn],ls[maxn];
int p,n;
int jc[maxn];
int isl[maxn];
inline LL inv(LL x) {
int k = p - 2;
int ret = 1;
for(;k;k >>= 1,x = 1ll * x * x % p)
if(k & 1) ret = 1ll * ret * x % p;
return ret;
}
int C (int x,int y) {
if(x < y) return 0;
if(x < p) return 1ll * jc[x] * inv(jc[y]) % p * inv(jc[x - y]) % p;
return 1ll * C(x / p,y / p) * C(x % p,y % p) % p;
}
int F(int x) {
if(f[x]) return f[x];
return 1ll * F(ls[x]) * F(x - 1 - ls[x]) % p * C(x - 1,ls[x]) % p;
}
main() {
n = read(); p = read();
f[1] = f[2] = 1;
isl[2] = 1;
for(int i = 3;i <= n;++ i) isl[i] = isl[i >> 1];
for(int i = 2;i <= n;++ i) ls[i] = ls[i - 1] + isl[i];
jc[0] = 1;
for(int i = 1;i <= n;++ i) jc[i] = jc[i - 1] * i % p;
printf("%lld\n",F(n));
return 0;
}
?
标签:const 大小 ret printf ons include || geo 处理
原文地址:https://www.cnblogs.com/sssy/p/9643354.html