f[i]=f[l]*f[r]*C(size[l]+size[r],size[l]).
需要lucas.
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 1000050 using namespace std; long long n,mod,inv1[maxn],inv2[maxn],f[maxn],size[maxn]; long long f_pow(long long x,long long y) { long long ans=1,base=x; while (y) { if (y&1) ans=(ans*base)%mod; base=(base*base)%mod; y>>=1; } return ans; } void get_table() { inv1[0]=1;inv2[0]=mod+1; for (long long i=1;i<=n;i++) { inv1[i]=inv1[i-1]*i%mod; inv2[i]=f_pow(inv1[i],mod-2); } } long long comb(long long n,long long m) { return inv1[n]*inv2[m]%mod*inv2[n-m]%mod; } long long lucas(long long n,long long m) { if (!m) return 1; return comb(n%mod,m%mod)*lucas(n/mod,m/mod)%mod; } int main() { scanf("%lld%lld",&n,&mod); get_table(); for (long long i=n;i>=1;i--) { size[i]=1;long long l=0,r=0;f[i]=1; if ((i<<1)<=n) {size[i]+=size[i<<1];l=size[i<<1];f[i]=f[i]*f[i<<1]%mod;} if ((i<<1|1)<=n) {size[i]+=size[i<<1|1];r=size[i<<1|1];f[i]=f[i]*f[i<<1|1]%mod;} f[i]=f[i]*lucas(l+r,l)%mod; } printf("%lld\n",f[1]%mod); return 0; }