結果

問題 No.117 組み合わせの数
ユーザー tatanaideyotatanaideyo
提出日時 2018-09-25 09:48:30
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 137 ms / 5,000 ms
コード長 3,645 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 164,848 KB
実行使用メモリ 18,944 KB
最終ジャッジ日時 2024-04-10 08:09:07
合計ジャッジ時間 2,270 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 137 ms
18,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:95:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   95 |     scanf("%d\n", &T);
      |     ~~~~~^~~~~~~~~~~~
main.cpp:98:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   98 |         scanf("%c(%d,%d)\n", &c, &n.v, &k.v);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>

using Int = int;

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;
}


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] = ModInt(i) * fact[i - 1];
        inv_f[n] = fact[n].inv();
        for (int i = n - 1; 0 <= i; --i) inv_f[i] = ModInt(i + 1) * inv_f[i + 1];
    }

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

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

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

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

        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