serna37's Library

Logo

C++ アルゴリズムとデータ構造のライブラリ

View the Project on GitHub serna37/library-cpp

:heavy_check_mark: 組み合わせ nCk
(library/number/combination.hpp)

組み合わせ nCk

できること

計算量

$O(k)$

使い方

long long comb = combination(n, k);

Verified with

Code

#pragma once
long long combination(int n, int k) {
    if (k < 0 || n < k) return 0ll;
    long long res = 1;
    for (int i = 1; i <= k; ++i) {
        res *= n--;
        res /= i;
    }
    return res;
}
#line 2 "library/number/combination.hpp"
long long combination(int n, int k) {
    if (k < 0 || n < k) return 0ll;
    long long res = 1;
    for (int i = 1; i <= k; ++i) {
        res *= n--;
        res /= i;
    }
    return res;
}
Back to top page