1 #include<iostream> 2 #include<cstdio> 3 #define ll long long 4 #define MAXN 2000010 5 using namespace std; 6 int n; 7 ll p; 8 ll jc[MAXN]; 9 ll dp[MAXN]; 10 int size[MAXN]; 11 ll pow(ll a,ll b){ 12 ll ans=1; 13 while(b){ 14 if(b&1)ans=(ans*a)%p; 15 a=(a*a)%p; 16 b>>=1; 17 } 18 return ans%p; 19 } 20 ll C(int a,int b){ 21 if(a<b)return 0; 22 if(b==0)return 1; 23 return jc[a]*pow(jc[a-b]*jc[b]%p,p-2)%p; 24 } 25 ll lucas(int a,int b){ 26 if(b>a)return 0; 27 if(b==0)return 1; 28 if(a>p||b>p)return C(a%p,b%p)*lucas(a/p,b/p)%p; 29 return C(a,b)%p; 30 } 31 void dfs(ll u){ 32 if(u>n){ 33 dp[u]=1; 34 return ; 35 } 36 dfs(u<<1); 37 dfs(u<<1|1); 38 size[u]=size[u<<1]+size[u<<1|1]+1; 39 dp[u]=dp[u<<1]*dp[u<<1|1]%p*lucas(size[u]-1,size[u<<1])%p; 40 return ; 41 } 42 int main(){ 43 scanf("%d%lld",&n,&p); 44 jc[0]=1; 45 for(int i=1;i<=n;++i)jc[i]=jc[i-1]*1ll*i%p; 46 dfs(1); 47 printf("%lld\n",dp[1]%p); 48 }
1 #include<iostream> 2 #include<cstdio> 3 #define ll long long 4 #define MAXN 2000010 5 using namespace std; 6 int n; 7 ll p; 8 ll jc[MAXN]; 9 ll dp[MAXN]; 10 int size[MAXN]; 11 ll pow(ll a,ll b){ 12 ll ans=1; 13 while(b){ 14 if(b&1)ans=(ans*a)%p; 15 a=(a*a)%p; 16 b>>=1; 17 } 18 return ans%p; 19 } 20 ll C(int a,int b){ 21 if(a<b)return 0; 22 if(b==0)return 1; 23 return jc[a]*pow(jc[a-b]*jc[b]%p,p-2)%p; 24 } 25 ll lucas(int a,int b){ 26 if(b>a)return 0; 27 if(b==0)return 1; 28 if(a>p||b>p)return C(a%p,b%p)*lucas(a/p,b/p)%p; 29 return C(a,b)%p; 30 } 31 void dfs(ll u){ 32 if(u>n){ 33 dp[u]=1; 34 return ; 35 } 36 dfs(u<<1); 37 dfs(u<<1|1); 38 size[u]=size[u<<1]+size[u<<1|1]+1; 39 dp[u]=dp[u<<1]*dp[u<<1|1]%p*lucas(size[u]-1,size[u<<1])%p; 40 return ; 41 } 42 int main(){ 43 scanf("%d%lld",&n,&p); 44 jc[0]=1; 45 for(int i=1;i<=n;++i)jc[i]=jc[i-1]*1ll*i%p; 46 dfs(1); 47 printf("%lld\n",dp[1]%p); 48 }