code:
#include <bits/stdc++.h> #define N 2000004 #define LL long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; LL mod; LL fac[N],inv[N],f[N],size[N]; LL qpow(LL x,LL y) { LL tmp=1ll; for(;y;y>>=1,x=x*x%mod) if(y&1) tmp=tmp*x%mod; return tmp; } LL C(int n, int m) { if (n<m) return 0ll; if (n<mod && m<mod) return fac[n]*inv[m]%mod*inv[n-m]%mod; return C(n/mod,m/mod)*C(n%mod,m%mod); } int main() { // setIO("input"); int i,j,n; scanf("%d%lld",&n,&mod); fac[0]=inv[0]=1ll; for(i=1;i<=n;++i) fac[i]=fac[i-1]*1ll*i%mod,inv[i]=qpow(fac[i],mod-2); for(i=0;i<=2*n;++i) f[i]=1ll; for(i=n;i>0;i--){ size[i]++; size[i/2]+=size[i]; } for(i=n;i>=1;--i) { if(size[i]==1) continue; else { f[i]=C(size[i]-1,size[i*2])*f[i*2]%mod*f[i*2+1]%mod; // printf("%d %d\n",size[i]-1,size[i*2]); // printf("%lld %lld %lld\n",C(i-1,size[i*2]),f[i*2],f[i*2+1]); // printf("%lld\n",f[i]); } } printf("%lld\n",f[1]); return 0; }
原文地址:https://www.cnblogs.com/guangheli/p/11768745.html
时间: 2024-10-07 16:12:26