可以发现这样的一个大于关系构成了一个树结构,而且是一颗线段树式的结构,满足堆性质
设f[i]表示i这个点所代表的子树满足要求的方案数,s[i]为以i为根的子树大小,那么f[i]=f[i<<1]*f[i<<1|1]*C(s[i]-1,s[i<<1]),即把最小的那个数拎出来后剩下的数随机分配
f[]和s[]一遍树归就都干掉了,取模的话加个逆元即可
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define LL unsigned long long
using namespace std;
const int maxn=1000010;
int n,mod,s[maxn];
LL fac[maxn],f[maxn];
LL powe(LL x,LL y){
LL ret=1;
while (y){
if (y&1) ret=ret*x%mod;
x=x*x%mod; y>>=1;
}return ret;
}
LL C(int x,int y){
return fac[x]*(powe(fac[x-y]*fac[y]%mod,mod-2))%mod;
}
LL tree_dp(int x){
if (f[x]) return f[x];
if ((x<<1)<=n) tree_dp(x<<1),s[x]+=s[x<<1];
if (((x<<1)|1)<=n) tree_dp(x<<1|1),s[x]+=s[x<<1|1];
if (((x<<1)>n)&&(((x<<1)|1)>n))
{s[x]=1;f[x]=1;return f[x];}
else if (((x<<1)>n)||(((x<<1)|1)>n)) f[x]=1;
else f[x]=(f[x<<1]*f[x<<1|1]%mod)*C(s[x]-1,s[x<<1])%mod;
return f[x];
}
int main(){
scanf("%d%d",&n,&mod); fac[1]=1; s[1]=1;
if (mod==2){printf("1");return 0;}
for (int i=2;i<=n;++i) fac[i]=fac[i-1]*i%mod,s[i]=1;
tree_dp(1);
printf("%lld\n",f[1]);
}