组合数
C n m C_{n}^{m} Cnm也写做 ( n m ) \tbinom{n}{m} (mn),其实 ( n m ) \tbinom{n}{m} (mn)比较正宗,但是习惯上还是喜欢 C n m C_{n}^{m} Cnm
C
n
m
=
n
!
m
!
(
n
−
m
!
)
C_{n}^{m}=\frac{n!}{m!(n-m!)}
Cnm=m!(n−m!)n!
求和公式(
k
=
1.2.
⋯
n
k=1.2.\cdots n
k=1.2.⋯n):
S
(
k
)
=
n
(
n
+
1
)
2
S(k)=\cfrac{n(n+1)}{2}
S(k)=2n(n+1)
S
(
2
k
−
1
)
=
n
2
S(2k-1)=n^2
S(2k−1)=n2
S
(
k
2
)
=
n
(
n
+
1
)
(
2
n
+
1
)
6
S(k^2)=\cfrac{n(n+1)(2n+1)}{6}
S(k2)=6n(n+1)(2n+1)
S
(
(
2
k
−
1
)
2
)
=
n
(
4
n
2
−
1
)
3
S((2k-1)^2)=\cfrac{n(4n^2-1)}{3}
S((2k−1)2)=3n(4n2−1)
S
(
k
3
)
=
n
2
(
2
n
2
−
1
)
S(k^3)=n^2(2n^2-1)
S(k3)=n2(2n2−1)
S
(
(
2
k
−
1
)
3
)
=
n
2
(
2
n
2
−
1
)
S((2k-1)^3)=n^2(2n^2-1)
S((2k−1)3)=n2(2n2−1)
S
(
k
4
)
=
n
(
n
+
1
)
(
2
n
+
1
)
(
3
n
2
+
3
n
−
1
)
30
S(k^4)=\cfrac{n(n+1)(2n+1)(3n^2+3n-1)}{30}
S(k4)=30n(n+1)(2n+1)(3n2+3n−1)
S
(
k
5
)
=
n
2
(
n
+
1
)
2
(
2
n
2
+
2
n
−
1
)
12
S( k^5 ) = \cfrac{n^2(n+1)^2(2n^2+2n-1)}{12}
S(k5)=12n2(n+1)2(2n2+2n−1)
S
(
k
(
k
+
1
)
)
=
n
(
n
+
1
)
(
n
+
2
)
3
S( k(k+1) ) = \cfrac{n(n+1)(n+2)}{3}
S(k(k+1))=3n(n+1)(n+2)
S
(
k
(
k
+
1
)
(
k
+
2
)
)
=
n
(
n
+
1
)
(
n
+
2
)
(
n
+
3
)
4
S( k(k+1)(k+2) ) = \cfrac{n(n+1)(n+2)(n+3)}{4}
S(k(k+1)(k+2))=4n(n+1)(n+2)(n+3)
S
(
k
(
k
+
1
)
(
k
+
2
)
(
k
+
3
)
)
=
n
(
n
+
1
)
(
n
+
2
)
(
n
+
3
)
(
n
+
4
)
5
S( k(k+1)(k+2)(k+3) ) = \cfrac{n(n+1)(n+2)(n+3)(n+4)}{5}
S(k(k+1)(k+2)(k+3))=5n(n+1)(n+2)(n+3)(n+4)
常见公式:
C
m
n
=
C
m
m
−
n
C_{m}^{n}=C_{m}^{m-n}
Cmn=Cmm−n
C m n C n r = C m r C m − r n − r C_{m}^{n}C_{n}^{r}=C_{m}^{r}C_{m-r}^{n-r} CmnCnr=CmrCm−rn−r
C m n = C m − 1 n + C m − 1 n − 1 C_{m}^{n}=C_{m-1}^{n}+C_{m-1}^{n-1} Cmn=Cm−1n+Cm−1n−1
C m 0 − C m 1 + C m 2 − ⋯ ± C m m = 0 C_{m}^{0}-C_{m}^{1}+C_{m}^{2}-\cdots\ \pm\ C_{m}^{m}=0 Cm0−Cm1+Cm2−⋯ ± Cmm=0
∑ i = 0 m C m i x i = ( x + 1 ) m \sum_{i=0}^{m}C_{m}^{i}x_i=(x+1)^m ∑i=0mCmixi=(x+1)m
C m + r + 1 r = ∑ i = 0 r C m + i i C_{m+r+1}^{r}=\sum_{i=0}^{r}C_{m+i}^{i} Cm+r+1r=∑i=0rCm+ii
∑ i = 0 m C m i = 2 m \sum_{i=0}^{m}C_{m}^{i}=2^m ∑i=0mCmi=2m
递推法代码
int comb[maxn][maxn];
const int MOD = 1e9+7;
int main(){
for(int i = 0; i < maxn; ++i){
comb[i][0] = comb[i][i] = 1;
for(int j = 1; j < i; ++j){
comb[i][j] = comb[i-1][j] + comb[i-1][j-1];
comb[i][j] %= MOD;
}
}
}
但是明显复杂度高达 O ( n 2 ) O(n^{2}) O(n2)
在 O ( n ) O(n) O(n)时间内求阶乘
typedef long long ll;
const int maxn = 50000;
const double INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
ll F[maxn],Finv[maxn],inv[maxn];//F是阶乘,Finv是逆元的阶乘
void init(){
inv[1] = 1;
for(ll i = 2; i < maxn; ++i){
inv[i] = (MOD-MOD/i)*inv[MOD%i]%MOD;
}
F[0] = Finv[0] = 1;
for(ll i = 1; i < maxn; ++i){
F[i] = F[i-1]*i%MOD;
Finv[i] = Finv[i-1]*inv[i]%MOD;
}
}
ll comb(int n,int m){//C[n][m]
if(m < 0 || m > n) return 0;
return F[n]*Finv[n-m]%MOD*Finv[m]%MOD;
}
int main(){
init();
return 0;
}