bzoj 2111: [ZJOI2010]Perm 排列计数

神题。。。

扒自某神犇题解:

http://blog.csdn.net/aarongzk/article/details/50655471

 1 #include<bits/stdc++.h>
 2 #define LL long long
 3 using namespace std;
 4 inline LL ra()
 5 {
 6     LL x=0; char ch=getchar();
 7     while (ch<'0' || ch>'9') ch=getchar();
 8     while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
 9     return x;
10 }
11 
12 const int maxn=2000005;
13 
14 LL n,mod,f[maxn],size[maxn];
15 LL fac[maxn],inv[maxn];
16 
17 LL C(int n, int m)
18 {
19     if (n<m) return 0;
20     if (n<mod && m<mod) return fac[n]*inv[m]%mod*inv[n-m]%mod;
21     return C(n/mod,m/mod)*C(n%mod,m%mod);
22 }
23 
24 int main()
25 {
26     n=ra(); mod=ra(); fac[0]=1;
27     for (int i=1; i<=n; i++) fac[i]=fac[i-1]*i%mod;
28     inv[0]=inv[1]=1;
29     for (int i=2; i<=n; i++) inv[i]=(mod-mod/i)*inv[mod%i]%mod;
30     for (int i=2; i<=n; i++) inv[i]=inv[i]*inv[i-1]%mod;
31     for (int i=n; i>=1; i--)
32     {
33         size[i]=size[i<<1]+size[i<<1|1]+1;
34         f[i]=((i<<1)>n?1:f[i<<1])*((i<<1|1)>n?1:f[i<<1|1])%mod*C(size[i]-1,size[i<<1])%mod;
35     }    
36     cout<<f[1]<<endl;
37     return 0;
38 }

 

posted @ 2017-04-20 10:19  ws_ccd  阅读(123)  评论(0编辑  收藏  举报