bzoj2111-dp/Lucas定理

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<string>
 5 #include<cstring>
 6 #include<algorithm>
 7 #include<iomanip>
 8 using namespace std;
 9 //f[i]:以i为根的完全二叉树个数。
10 //f[i]=f[i<<1]*f[i<<1|1]*c[s[i]-1,i<<1]; 
11 namespace Moxing{
12     const int N=5e6+5;
13     int n,p;
14     long long inv[N],jie[N],f[N],size[N];
15     long long power(long long a,long long b){
16         long long ans=1;
17         while(b){
18             if(b&1) ans=(ans*a)%p;
19             a=(a*a)%p,b>>=1;
20         } 
21         return ans;
22     }
23     void first(int n){
24         jie[1]=inv[1]=inv[0]=1;
25         for(int i=2;i<=n;i++){
26             jie[i]=(long long)(jie[i-1]*i)%p;
27             inv[i]=power(jie[i],p-2);
28         }
29     }
30     long long c(int n,int m){
31         if(n<m) return 0;
32         return (long long)jie[n]*inv[n-m]%p*inv[m]%p;
33     }
34     long long lucas(int n,int m){
35         if(!n&&!m) return 1;
36         return (long long)(c(n%p,m%p)*lucas(n/p,m/p))%p;
37     }
38     struct main{
39         main(){
40             scanf("%d%d",&n,&p);
41             first(n);
42             for(int i=n;i;i--){
43                 size[i] = size[i<<1] + s[i << 1|1] + 1;
44                 f[i] = lucas(size[i]-1, size[i<<1]);
45                 if(i << 1 <= n) f[i] = (long long)f[i] * f[i<<1] % p;
46                 if((i << 1 | 1) <= n) f[i] = (long long)f[i] * f[i<<1|1] % p;
47             }
48             printf("%lld\n",f[1]);
49             exit(0);
50         } 
51     }UniversalLove; 
52 }
53 int main(){
54     Moxing::main();
55 }
View Code

 

-----------兴天下之利,除天下之害。-----------
posted @ 2019-08-15 21:24  Moxingtianxia  阅读(73)  评论(0编辑  收藏  举报