Kyopro Library
読み取り中…
検索中…
一致する文字列を見つけられません
combination.hpp
[詳解]
1
#
include
"../../kyopro_library/template.hpp"
2
3
/// @brief 二項係数・階乗計算
4
template
<
typename
T>
5
struct
Combinatorics
{
6
Combinatorics
()=
default
;
7
8
/// @brief 二項係数の前計算
9
/// @note O(N)
10
Combinatorics
(
int
n) {
11
fac=vector<T>(n+1);
12
finv=vector<T>(n+1);
13
fac[0]=1;
14
for
(
int
i=1; i<=n; i++) fac[i]=fac[i-1]*i;
15
finv[n]=fac[n].inv();
16
for
(
int
i=n; i>=1; i--) finv[i-1]=finv[i]*i;
17
}
18
19
/// @brief nCr を返す
20
/// @note n < 0, r < 0, n < r のときは 0 を返す
21
T
comb
(ll n, ll r) {
22
if
(n<0||r<0||n-r<0)
return
0;
23
return
fac[n]*finv[r]*finv[n-r];
24
}
25
26
/// @brief nPr を返す
27
/// @note n < 0, r < 0, n < r のときは 0 を返す
28
T
perm
(ll n, ll r) {
29
if
(n<0||r<0||n-r<0)
return
0;
30
return
fac[n]*finv[n-r];
31
}
32
33
/// @brief n! を返す
34
T
factrial
(
int
n) {
return
fac[n]; }
35
36
/// @brief (n!)^-1 を返す
37
T
factinv
(
int
n) {
return
finv[n]; }
38
39
/// @brief nCr を返す
40
T
operator
()(ll n, ll r) {
return
comb
(
n
,
r
)
; }
41
42
private
:
43
vector<T> fac,finv;
44
};
Combinatorics
二項係数・階乗計算
Definition
combination.hpp:5
Combinatorics::Combinatorics
Combinatorics()=default
Combinatorics::factrial
T factrial(int n)
n! を返す
Definition
combination.hpp:34
Combinatorics::comb
T comb(ll n, ll r)
nCr を返す
Definition
combination.hpp:21
Combinatorics::operator()
T operator()(ll n, ll r)
nCr を返す
Definition
combination.hpp:40
Combinatorics::factinv
T factinv(int n)
(n!)^-1 を返す
Definition
combination.hpp:37
Combinatorics::perm
T perm(ll n, ll r)
nPr を返す
Definition
combination.hpp:28
Combinatorics::Combinatorics
Combinatorics(int n)
二項係数の前計算
Definition
combination.hpp:10
mod
combination.hpp
構築:
1.13.2