結果
問題 | No.117 組み合わせの数 |
ユーザー | tatanaideyo |
提出日時 | 2018-09-25 10:53:54 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 77 ms / 5,000 ms |
コード長 | 4,026 bytes |
コンパイル時間 | 1,625 ms |
コンパイル使用メモリ | 172,024 KB |
実行使用メモリ | 18,920 KB |
最終ジャッジ日時 | 2024-10-02 13:06:50 |
合計ジャッジ時間 | 2,157 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ソースコード
#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=(const ModInt &r) { if (this != &r) v = set(r.v); return *this; } 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) { v = ((unsigned long long)v * r.v) % mod; return *this; } ModInt &operator/=(ModInt r) { *this *= r.inv(); return *this; } ModInt operator+(ModInt r) const { return ModInt(*this) += r; } ModInt operator-(ModInt r) const { return ModInt(*this) -= r; } ModInt operator*(ModInt r) const { return ModInt(*this) *= r; } ModInt operator/(ModInt r) const { return ModInt(*this) /= r; } 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); } }; 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 mod = ModInt::mod; const int N; std::vector<ModInt> fact, inv_f; // MultisetCoefficient を使用する場合は n = 2 * _n とする Combination(int _n) : N(_n < mod ? _n : mod - 1), fact(N + 1), inv_f(N + 1) { fact[0] = 1; for (int i = 1; i <= N; ++i) fact[i] = fact[i - 1] * i; inv_f[N] = fact[N].inv(); for (int i = N; 1 <= i; --i) inv_f[i - 1] = inv_f[i] * 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; }