fi
表示
i
个数组成的小根堆的种类,
fi=Csizei<<1sizei−1fi<<1fi<<1|1
#include<iostream>
#include<cstdio>
#define N 1000005
#define ll long long
using namespace std;
int n,P;
int f[N<<1],size[N<<1],fac[N],inv[N];
inline int read()
{
int a=0,f=1; char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();}
while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();}
return a*f;
}
inline void pre()
{
fac[0]=fac[1]=1; inv[0]=inv[1]=1;
for (int i=2;i<=n;i++) fac[i]=(ll)fac[i-1]*i%P;
for (int i=2;i<=n;i++) inv[i]=(ll)(P-P/i)*inv[P%i]%P;
for (int i=2;i<=n;i++) inv[i]=(ll)inv[i]*inv[i-1]%P;
}
inline int C(int n,int m)
{
if (n<m) return 0;
if (n<P&&m<P) return (ll)fac[n]*inv[m]%P*inv[n-m]%P;
return C(n/P,m/P)*C(n%P,m%P)%P;
}
int main()
{
n=read(); P=read();
pre();
for (int i=n+1;i<=2*n+1;i++) f[i]=1,size[i]=0;
for (int i=n;i;i--)
{
size[i]=size[i<<1]+size[i<<1|1]+1;
f[i]=(ll)C(size[i]-1,size[i<<1])*f[i<<1]%P*f[i<<1|1]%P;
}
cout << f[1] << endl;
return 0;
}