Skip to content

Commit 9b552e3

Browse files
committed
Binomial coefficient test
1 parent 9e9cfe2 commit 9b552e3

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

cp-algo/math/combinatorics.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#ifndef CP_ALGO_MATH_COMBINATORICS_HPP
22
#define CP_ALGO_MATH_COMBINATORICS_HPP
33
#include "common.hpp"
4+
#include <cassert>
45
namespace cp_algo::math {
56
template<typename T>
67
T binom_large(T n, int r) {
7-
assert(r < math::maxn);
8+
assert(r < maxn);
89
T ans = 1;
910
for(int i = 0; i < r; i++) {
1011
ans = ans * T(n - i) * small_inv<T>(i + 1);
@@ -15,10 +16,10 @@ namespace cp_algo::math {
1516
T binom(int n, int r) {
1617
if(r < 0 || r > n) {
1718
return T(0);
18-
} else if(n > math::maxn) {
19-
return binom_large(n, r);
19+
} else if(n >= maxn) {
20+
return binom_large(T(n), r);
2021
} else {
21-
return math::fact<T>(n) * math::rfact<T>(r) * math::rfact<T>(n-r);
22+
return fact<T>(n) * rfact<T>(r) * rfact<T>(n - r);
2223
}
2324
}
2425
}

cp-algo/math/common.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ namespace cp_algo::math {
2929
T bpow(T const& x, int64_t n) {
3030
return bpow(x, n, T(1));
3131
}
32+
// fact/rfact/small_inv are caching
33+
// Beware of usage with dynamic mod
3234
template<typename T>
3335
T fact(int n) {
3436
static std::vector<T> F(maxn);
@@ -42,13 +44,15 @@ namespace cp_algo::math {
4244
}
4345
return F[n];
4446
}
47+
// Only works for modint types
4548
template<typename T>
4649
T rfact(int n) {
4750
static std::vector<T> F(maxn);
4851
static bool init = false;
4952
if(!init) {
50-
F[maxn - 1] = T(1) / fact<T>(maxn - 1);
51-
for(int i = maxn - 2; i >= 0; i--) {
53+
int t = std::min<int64_t>(T::mod(), maxn) - 1;
54+
F[t] = T(1) / fact<T>(t);
55+
for(int i = t - 1; i >= 0; i--) {
5256
F[i] = F[i + 1] * T(i + 1);
5357
}
5458
init = true;

verify/combi/binom.test.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// @brief Binomial Coefficient (Prime Mod)
2+
#define PROBLEM "https://judge.yosupo.jp/problem/binomial_coefficient_prime_mod"
3+
#pragma GCC optimize("Ofast,unroll-loops")
4+
#pragma GCC target("avx2,tune=native")
5+
#define CP_ALGO_MAXN 1e7
6+
#include "cp-algo/math/modint.hpp"
7+
#include "cp-algo/math/combinatorics.hpp"
8+
#include <bits/stdc++.h>
9+
10+
using namespace std;
11+
using namespace cp_algo;
12+
using namespace math;
13+
using base = dynamic_modint;
14+
15+
void solve() {
16+
int n, r;
17+
cin >> n >> r;
18+
cout << binom<base>(n, r) << "\n";
19+
}
20+
21+
signed main() {
22+
//freopen("input.txt", "r", stdin);
23+
ios::sync_with_stdio(0);
24+
cin.tie(0);
25+
int t = 1;
26+
cin >> t;
27+
int m;
28+
cin >> m;
29+
base::switch_mod(m);
30+
while(t--) {
31+
solve();
32+
}
33+
}

0 commit comments

Comments
 (0)