proconlib

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub KodamaD/proconlib

:heavy_check_mark: test/fp_util.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/117"
#include "../math/static_modint.cpp"
#include "../math/fp_util.cpp"
#include "../utility/int_alias.cpp"
#include <iostream>

using Fp = StaticModint<1000000007>;
using Util = FpUtil<Fp>;

int main() {
    int T;
    std::cin >> T;
    while (T--) {
        char type, dust;
        int N, K;
        std::cin >> type >> dust >> N >> dust >> K >> dust;
        if (type == 'C') {
            if (N < K) std::cout << 0 << '\n';
            else std::cout << Util::binom(N, K) << '\n';
        }
        if (type == 'P') {
            if (N < K) std::cout << 0 << '\n';
            else std::cout << Util::factpow(N, K) << '\n';
        }
        if (type == 'H') {
            if (N == 0 && K > 0) std::cout << 0 << '\n';
            else std::cout << Util::homo(N, K) << '\n';
        }
    }
    return 0;
}
#line 1 "test/fp_util.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/117"
#line 2 "math/static_modint.cpp"
#include <ostream>
#include <type_traits>
#line 2 "utility/int_alias.cpp"
#include <cstdint>

using i32 = std::int32_t;
using u32 = std::uint32_t;
using i64 = std::int64_t;
using u64 = std::uint64_t;
using i128 = __int128_t;
using u128 = __uint128_t;
#line 2 "math/rem_euclid.cpp"
#include <cassert>

template <class T> constexpr T rem_euclid(T value, const T& mod) {
    assert(mod > 0);
    return (value %= mod) >= 0 ? value : value + mod;
}
#line 2 "math/totient.cpp"

template <class T> constexpr T totient(T x) {
    T ret = x;
    for (T i = 2; i * i <= x; ++i) {
        if (x % i == 0) {
            ret /= i;
            ret *= i - 1;
            while (x % i == 0) x /= i;
        }
    }
    if (x > 1) {
        ret /= x;
        ret *= x - 1;
    }
    return ret;
}
#line 7 "math/static_modint.cpp"

template <u32 MOD, std::enable_if_t<((u32)1 <= MOD and MOD <= ((u32)1 << 31))>* = nullptr> class StaticModint {
    using Self = StaticModint;

    static inline constexpr u32 PHI = totient(MOD);
    u32 v;

  public:
    static constexpr u32 mod() noexcept { return MOD; }

    template <class T, std::enable_if_t<std::is_integral_v<T>>* = nullptr>
    static constexpr T normalize(const T& x) noexcept {
        return rem_euclid<std::common_type_t<T, i64>>(x, MOD);
    }

    constexpr StaticModint() noexcept : v(0) {}
    template <class T> constexpr StaticModint(const T& x) noexcept : v(normalize(x)) {}
    template <class T> static constexpr Self raw(const T& x) noexcept {
        Self ret;
        ret.v = x;
        return ret;
    }

    constexpr u32 val() const noexcept { return v; }
    constexpr Self neg() const noexcept { return raw(v == 0 ? 0 : MOD - v); }
    constexpr Self inv() const noexcept { return pow(PHI - 1); }
    constexpr Self pow(u64 exp) const noexcept {
        Self ret(1), mult(*this);
        for (; exp > 0; exp >>= 1) {
            if (exp & 1) ret *= mult;
            mult *= mult;
        }
        return ret;
    }

    constexpr Self operator-() const noexcept { return neg(); }
    constexpr Self operator~() const noexcept { return inv(); }

    constexpr Self operator+(const Self& rhs) const noexcept { return Self(*this) += rhs; }
    constexpr Self& operator+=(const Self& rhs) noexcept {
        if ((v += rhs.v) >= MOD) v -= MOD;
        return *this;
    }

    constexpr Self operator-(const Self& rhs) const noexcept { return Self(*this) -= rhs; }
    constexpr Self& operator-=(const Self& rhs) noexcept {
        if (v < rhs.v) v += MOD;
        v -= rhs.v;
        return *this;
    }

    constexpr Self operator*(const Self& rhs) const noexcept { return Self(*this) *= rhs; }
    constexpr Self& operator*=(const Self& rhs) noexcept {
        v = (u64)v * rhs.v % MOD;
        return *this;
    }

    constexpr Self operator/(const Self& rhs) const noexcept { return Self(*this) /= rhs; }
    constexpr Self& operator/=(const Self& rhs) noexcept { return *this *= rhs.inv(); }

    constexpr bool operator==(const Self& rhs) const noexcept { return v == rhs.v; }
    constexpr bool operator!=(const Self& rhs) const noexcept { return v != rhs.v; }
    friend std::ostream& operator<<(std::ostream& stream, const Self& rhs) { return stream << rhs.v; }
};

using Modint1000000007 = StaticModint<1000000007>;
using Modint998244353 = StaticModint<998244353>;
#line 3 "math/fp_util.cpp"
#include <vector>

template <class M> struct FpUtil {
    static M fact(const int n) {
        static std::vector<M> vec;
        assert(n >= 0);
        if (vec.empty()) vec = {M(1)};
        for (int i = vec.size(); i <= n; ++i) vec.push_back(vec.back() * M(i));
        return vec[n];
    }
    static M inv(const int n) {
        static std::vector<M> vec;
        assert(n > 0);
        if (vec.empty()) vec = {M(0), M(1)};
        for (int i = vec.size(); i <= n; ++i) vec.push_back(-M(M::mod() / i) * vec[M::mod() % i]);
        return vec[n];
    }
    static M inv_fact(const int n) {
        static std::vector<M> vec;
        assert(n >= 0);
        if (vec.empty()) vec = {M(1)};
        for (int i = vec.size(); i <= n; ++i) vec.push_back(vec.back() * inv(i));
        return vec[n];
    }
    static M binom(const int n, const int k) {
        assert(0 <= k and k <= n);
        return fact(n) * inv_fact(n - k) * inv_fact(k);
    }
    static M factpow(const int n, const int k) {
        assert(0 <= k and k <= n);
        return fact(n) * inv_fact(n - k);
    }
    static M homo(const int n, const int k) {
        assert((n == 0 and k == 0) or (n > 0 and k >= 0));
        if (n == 0 and k == 0) return M(1);
        return binom(n + k - 1, k);
    }
};
#line 5 "test/fp_util.test.cpp"
#include <iostream>

using Fp = StaticModint<1000000007>;
using Util = FpUtil<Fp>;

int main() {
    int T;
    std::cin >> T;
    while (T--) {
        char type, dust;
        int N, K;
        std::cin >> type >> dust >> N >> dust >> K >> dust;
        if (type == 'C') {
            if (N < K) std::cout << 0 << '\n';
            else std::cout << Util::binom(N, K) << '\n';
        }
        if (type == 'P') {
            if (N < K) std::cout << 0 << '\n';
            else std::cout << Util::factpow(N, K) << '\n';
        }
        if (type == 'H') {
            if (N == 0 && K > 0) std::cout << 0 << '\n';
            else std::cout << Util::homo(N, K) << '\n';
        }
    }
    return 0;
}
Back to top page