Skip to the content.

:heavy_check_mark: test/yukicoder/117.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/117"
#include <bits/stdc++.h>

#include "library/mod/MintUtility.hpp"
#include "library/mod/Modint.hpp"

using mint = Mint<long long, 1000'000'007>;
MintUtility<mint> M;

mint solve() {
    std::string s;
    std::cin >> s;
    int comma = 0;
    while (s[comma] != ',')
        comma++;
    int n = stoi(s.substr(2, comma - 1));
    int k = stoi(s.substr(comma + 1, s.size() - comma - 1));
    if (s[0] == 'C')
        return M.nCk(n, k);
    if (s[0] == 'P')
        return M.nPk(n, k);
    return M.nHk(n, k);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int testsize;
    std::cin >> testsize;
    while (testsize--)
        std::cout << solve() << '\n';
}
#line 1 "test/yukicoder/117.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/117"
#include <bits/stdc++.h>

#line 1 "library/mod/MintUtility.hpp"
template <typename MINT> class MintUtility {
    std::vector<MINT> fact_ = {MINT::raw(1)};
    std::vector<MINT> inv_fact_{MINT::raw(1)};
    int S = 1; // 今のサイズ

    void extend(const int n) {
        if (n < S)
            return;
        const int preS = S;
        do {
            S <<= 1;
        } while (S <= n);

        fact_.resize(S);
        for (int i = preS; i < S; i++)
            fact_[i] = fact_[i - 1] * MINT::raw(i);

        inv_fact_.resize(S);
        inv_fact_.back() = fact_.back().inv();
        for (int i = S - 1; i > preS; i--)
            inv_fact_[i - 1] = inv_fact_[i] * MINT::raw(i);
    }

  public:
    MINT fact(const int n) {
        assert(n >= 0);
        extend(n);
        return fact_[n];
    }
    MINT inv_fact(const int n) {
        assert(n >= 0);
        extend(n);
        return inv_fact_[n];
    }
    MINT nCk(const int n, const int k) {
        if (k < 0 || n < k)
            return MINT::raw(0);
        extend(n);
        return fact_[n] * inv_fact_[k] * inv_fact_[n - k];
    }
    MINT nPk(const int n, const int k) {
        if (k < 0 || n < k)
            return MINT::raw(0);
        extend(n);
        return fact_[n] * inv_fact_[n - k];
    }
    MINT nHk(const int n, const int k) {
        return (n == 0 and k == 0 ? 1 : nCk(n + k - 1, k));
    }
};
#line 2 "library/math/ExtraGCD.hpp"
using ll = long long;
std::pair<ll, ll> ext_gcd(ll a, ll b) {
    if (b == 0)
        return {1, 0};
    auto [X, Y] = ext_gcd(b, a % b);
    // bX + (a%b)Y = gcd(a,b)
    // a%b = a - b(a/b)
    // ∴ aY + b(X-(a/b)Y) = gcd(a,b)
    ll x = Y, y = X - (a / b) * Y;
    return {x, y};
}
#line 3 "library/mod/Modint.hpp"
template <typename T, T MOD = 998244353> struct Mint {
    inline static constexpr T mod = MOD;
    T v;
    Mint() : v(0) {}
    Mint(signed v) : v(v) {}
    Mint(long long t) {
        v = t % MOD;
        if (v < 0)
            v += MOD;
    }

    static Mint raw(int v) {
        Mint x;
        x.v = v;
        return x;
    }

    Mint pow(long long k) const {
        Mint res(1), tmp(v);
        while (k) {
            if (k & 1)
                res *= tmp;
            tmp *= tmp;
            k >>= 1;
        }
        return res;
    }

    static Mint add_identity() { return Mint(0); }
    static Mint mul_identity() { return Mint(1); }

    // Mint inv()const{return pow(MOD-2);}
    Mint inv() const { return Mint(ext_gcd(v, mod).first); }

    Mint &operator+=(Mint a) {
        v += a.v;
        if (v >= MOD)
            v -= MOD;
        return *this;
    }
    Mint &operator-=(Mint a) {
        v += MOD - a.v;
        if (v >= MOD)
            v -= MOD;
        return *this;
    }
    Mint &operator*=(Mint a) {
        v = 1LL * v * a.v % MOD;
        return *this;
    }
    Mint &operator/=(Mint a) { return (*this) *= a.inv(); }

    Mint operator+(Mint a) const { return Mint(v) += a; }
    Mint operator-(Mint a) const { return Mint(v) -= a; }
    Mint operator*(Mint a) const { return Mint(v) *= a; }
    Mint operator/(Mint a) const { return Mint(v) /= a; }
#define FRIEND(op)                                                             \
    friend Mint operator op(int a, Mint b) { return Mint(a) op b; }
    FRIEND(+);
    FRIEND(-);
    FRIEND(*);
    FRIEND(/);
#undef FRIEND
    Mint operator+() const { return *this; }
    Mint operator-() const { return v ? Mint(MOD - v) : Mint(v); }

    bool operator==(const Mint a) const { return v == a.v; }
    bool operator!=(const Mint a) const { return v != a.v; }

    static Mint comb(long long n, int k) {
        Mint num(1), dom(1);
        for (int i = 0; i < k; i++) {
            num *= Mint(n - i);
            dom *= Mint(i + 1);
        }
        return num / dom;
    }

    friend std::ostream &operator<<(std::ostream &os, const Mint &m) {
        os << m.v;
        return os;
    }
    friend std::istream &operator>>(std::istream &is, Mint &m) {
        is >> m.v;
        m.v %= MOD;
        if (m.v < 0)
            m.v += MOD;
        return is;
    }
};
#line 6 "test/yukicoder/117.test.cpp"

using mint = Mint<long long, 1000'000'007>;
MintUtility<mint> M;

mint solve() {
    std::string s;
    std::cin >> s;
    int comma = 0;
    while (s[comma] != ',')
        comma++;
    int n = stoi(s.substr(2, comma - 1));
    int k = stoi(s.substr(comma + 1, s.size() - comma - 1));
    if (s[0] == 'C')
        return M.nCk(n, k);
    if (s[0] == 'P')
        return M.nPk(n, k);
    return M.nHk(n, k);
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int testsize;
    std::cin >> testsize;
    while (testsize--)
        std::cout << solve() << '\n';
}
Back to top page