結果

問題 No.117 組み合わせの数
ユーザー tatanaideyotatanaideyo
提出日時 2018-09-25 10:18:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 120 ms / 5,000 ms
コード長 3,828 bytes
コンパイル時間 1,818 ms
コンパイル使用メモリ 168,268 KB
実行使用メモリ 18,960 KB
最終ジャッジ日時 2024-04-10 09:13:49
合計ジャッジ時間 2,412 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
18,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h> // Replace needed headers for speed up

//  @require ./modular_arithmetics.cc 👇👇
using Int = int;
// using Int = long long;

struct ModInt {
    static constexpr Int mod = 1e9 + 7;
    Int v;

    ModInt(Int _v = 0) : v(set(_v)) {}
    ModInt(const ModInt &r) : v(set(r.v)) {}

    inline static Int set(const Int x) { return (x < 0 ? (x % mod) + mod : x % mod); }
    inline void set() { v = set(v); }

    bool operator<(ModInt r) const { return v < r.v; }
    bool operator>(ModInt r) const { return r.v < v; }
    bool operator==(ModInt r) const { return v == r.v; }
    bool operator!= (ModInt r) const { return v != r.v; }

    ModInt operator-() const { return ModInt(v ? mod - v : v); }
    ModInt &operator+=(ModInt r) { (v += r.v) %= mod; return *this; }
    ModInt &operator-=(ModInt r) { (v -= r.v - mod) %= mod; return *this; }
    ModInt &operator*=(ModInt r) { v = (__uint128_t(v) * r.v) % mod; return *this; }
    ModInt &operator/=(ModInt r) { *this *= r.inv(); return *this; }
    ModInt &operator=(const ModInt &r) { if (this != &r) v = set(r.v); return *this; }

    ModInt inv() const {
        Int u = 1, tv = v, s = 0, t = mod;
        while (t) {
            Int q = tv / t;
            std::swap(tv -= q * t, t);
            std::swap(u -= q * s, s);
        }
        return ModInt(u < 0 ? u + mod : u);
    }

    ModInt pow(Int e) {
        ModInt a = *this, x(1);
        for ( ; 0 < e; e >>= 1) { if (e & 1) x *= a; a *= a; }
        return x;
    }
    inline ModInt pow(ModInt &e) { return pow(e.v); }
};
ModInt operator+(ModInt l, ModInt r) { return l += r; }
ModInt operator-(ModInt l, ModInt r) { return l -= r; }
ModInt operator*(ModInt l, ModInt r) { return l *= r; }
ModInt operator/(ModInt l, ModInt r) { return l /= r; }
std::ostream &operator<<(std::ostream &os, const ModInt &r) { return os << r.v; }
std::istream &operator>>(std::istream &is, ModInt &r) { is >> r.v; r.set();return is; }

std::vector<ModInt> Inverse(const Int n = ModInt::mod - 1) {
    constexpr Int mod = ModInt::mod;
    std::vector<ModInt> inv(n + 1);
    inv[1].v = 1;
    for (Int a = 2; a <= n; ++a)
        inv[a] = inv[mod % a] * ModInt(mod - mod / a);
    return inv;
}


// -------------8<------- start of library -------8<------------------------
struct Combination {
    const int n;
    std::vector<ModInt> fact, inv_f;

    // MultisetCoefficient を使用する場合は n = 2 * _n とする
    Combination(int _n) : n(_n), fact(n + 1), inv_f(n + 1) {
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = i * fact[i - 1];
        inv_f[n] = fact[n].inv();
        for (int i = n; 1 <= i; --i) inv_f[i - 1] = i * inv_f[i];
    }

    ModInt Factorial(const int &n) const { return fact[n]; }
    ModInt InverseFactorial(const int &n) const { return inv_f[n]; }
    ModInt Permuation(const int &n, const int &k) const {
        if (k < 0 || n < k) return ModInt(0);
        else return fact[n] * inv_f[n - k];
    }
    ModInt BinomialCoefficient(const int &n, const int &k) const {
        if (n < 0 || k < 0 || n < k) return ModInt(0);
        else return fact[n] * inv_f[k] * inv_f[n - k];
    }
    ModInt MultisetCoefficient(const int &n, const int &k) const {
        if (n < 0 || k < 0) return ModInt(0);
        else return k == 0 ? 1 : BinomialCoefficient(n + k - 1, k);
    }
};
// -------------8<------- end of library ---------8-------------------------

int main() {
    Combination cm(2 * 1e6);

    int T, n, k;
    char c;
    scanf("%d\n", &T);

    while (T--) {
        scanf("%c(%d,%d)\n", &c, &n, &k);

        if (c == 'C') printf("%d\n", cm.BinomialCoefficient(n, k).v);
        else if (c == 'P') printf("%d\n", cm.Permuation(n, k).v);
        else if (c == 'H') printf("%d\n", cm.MultisetCoefficient(n, k).v);
    }

    return 0;
}
0