結果

問題 No.117 組み合わせの数
ユーザー rogi52rogi52
提出日時 2021-07-13 01:22:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 128 ms / 5,000 ms
コード長 3,254 bytes
コンパイル時間 1,526 ms
コンパイル使用メモリ 167,840 KB
実行使用メモリ 26,740 KB
最終ジャッジ日時 2023-09-14 21:39:32
合計ジャッジ時間 2,370 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

template< int mod >
struct Fp {
    int x;
    Fp() : x(0) {}
    Fp(int64_t y) : x(y >= 0 ? y % mod : (mod + y % mod) % mod) {}
    Fp &operator+=(const Fp &p) { if((x += p.x) >= mod) x -= mod; return *this; }
    Fp &operator-=(const Fp &p) { if((x += mod - p.x) >= mod) x -= mod; return *this; }
    Fp &operator*=(const Fp &p) { x = (int)(1LL * x * p.x % mod); return *this; }
    Fp &operator/=(const Fp &p) { *this *= p.inverse(); return *this; }
    Fp operator-() const { return Fp(-x); }
    Fp operator+(const Fp &p) const { return Fp(*this) += p; }
    Fp operator-(const Fp &p) const { return Fp(*this) -= p; }
    Fp operator*(const Fp &p) const { return Fp(*this) *= p; }
    Fp operator/(const Fp &p) const { return Fp(*this) /= p; }
    bool operator==(const Fp &p) const { return x == p.x; }
    bool operator!=(const Fp &p) const { return x != p.x; }

    Fp inv() const {
        int a = x, b = mod, u = 1, v = 0, t;
        while(b > 0) {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return Fp(u);
    }

    Fp pow(int64_t n) const {
        Fp ret(1), mul(x);
        while(n > 0) {
            if(n & 1) ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const Fp &p) { return os << p.x; }
    friend istream &operator>>(istream &is, Fp &a) {
        int64_t t;
        is >> t;
        a = Fp< mod > (t);
        return (is);
    }

    static int get_mod() { return mod; }
};

template<class T> struct Mod_package {
    vector< T > fact_, inv_, finv_;
    constexpr Mod_package(int n) noexcept : fact_(n, 1), inv_(n, 1), finv_(n, 1) {
        int mod = fact_[0].get_mod();
        for(int i = 2; i < n; i++){
            fact_[i] = fact_[i - 1] * i;
            inv_[i] = -inv_[mod % i] * (mod / i);
            finv_[i] = finv_[i - 1] * inv_[i];
        }
    }

    constexpr T comb(int n, int k) const noexcept {
        if(n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[k] * finv_[n - k];
    }

    constexpr T perm(int n, int k) const noexcept {
        if(n < k || n < 0 || k < 0) return 0;
        return fact_[n] * finv_[n - k];
    }

    constexpr T hom(int n, int k) const noexcept {
        if(n < 0 || k < 0 || (n == 0 && k > 0)) return 0;
        if(n == 0 && k == 0) return 1;
        return fact_[n + k - 1] *  finv_[k] * finv_[n - 1];
    }

    constexpr T fact(int n) const noexcept {
        if(n < 0) return 0; return fact_[n];
    }

    constexpr T inv(int n) const noexcept {
        if(n < 0) return 0; return inv_[n];
    }

    constexpr T finv(int n) const noexcept {
        if(n < 0) return 0; return finv_[n];
    }
};

using mint = Fp<1000000007>;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    Mod_package<mint> mp(2020202);
    int T; cin >> T;
    while(T--) {
        char c,d; int N,K;
        cin >> c >> d >> N >> d >> K >> d;
        if(c == 'C') printf("%d\n", mp.comb(N, K));
        if(c == 'P') printf("%d\n", mp.perm(N, K));
        if(c == 'H') printf("%d\n", mp.hom(N, K));
    }
}
0