結果

問題 No.117 組み合わせの数
ユーザー zawakasuzawakasu
提出日時 2024-03-06 21:12:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 396 ms / 5,000 ms
コード長 3,934 bytes
コンパイル時間 1,069 ms
コンパイル使用メモリ 106,072 KB
実行使用メモリ 50,200 KB
最終ジャッジ日時 2024-03-06 21:12:58
合計ジャッジ時間 2,481 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#line 1 "117.test.cpp"
#define PROBLEM "https://onlinejudge.u-aizu.ac.jp/courses/lesson/2/ITP1/1/ITP1_1_A"

#line 2 "/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp"

#line 2 "/home/zawatin/compro/cp-documentation/Src/Template/TypeAlias.hpp"

#include <cstdint>
#include <cstddef>

namespace zawa {

using i16 = std::int16_t;
using i32 = std::int32_t;
using i64 = std::int64_t;
using i128 = __int128_t;

using u8 = std::uint8_t;
using u16 = std::uint16_t;
using u32 = std::uint32_t;
using u64 = std::uint64_t;

using usize = std::size_t;

} // namespace zawa
#line 4 "/home/zawatin/compro/cp-documentation/Src/Template/IOSetting.hpp"

#include <iostream>
#include <iomanip>

namespace zawa {

void SetFastIO() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
}

void SetPrecision(u32 dig) {
    std::cout << std::fixed << std::setprecision(dig);
}

} // namespace zawa
#line 2 "/home/zawatin/compro/cp-documentation/Src/Number/BinomalCoefficients.hpp"

#line 4 "/home/zawatin/compro/cp-documentation/Src/Number/BinomalCoefficients.hpp"

#include <cassert>
#include <cmath>
#include <vector>
#include <atcoder/internal_math>
#include <atcoder/modint>

namespace zawa {

template <u32 MOD>
class BinomalCoefficients {
public:
    using Value = atcoder::static_modint<MOD>;
    static_assert(atcoder::internal::is_prime_constexpr(MOD));
private:
    usize n_{};
    std::vector<Value> F_{}, inv_{}, invF_{};
    constexpr bool need(usize n) const noexcept {
        return n_ <= n;
    }
    void expand(usize n) {
        F_.reserve(n + 1);
        inv_.reserve(n + 1);
        invF_.reserve(n + 1);
        for (usize i{n_} ; i <= n ; i++) {
            F_.emplace_back(F_.back() * Value{i});
            inv_.emplace_back(MOD - inv_[MOD % i] * (MOD / i));
            invF_.emplace_back(invF_.back() * inv_.back());
        }
        n_ = n + 1;
    }
public:
    constexpr usize size() const noexcept {
        return n_;
    }
    BinomalCoefficients() 
        : n_{2u}, F_{Value::raw(1), Value::raw(1)}, inv_{Value{}, Value::raw(1)}, 
        invF_{Value::raw(1), Value::raw(1)} {}
    BinomalCoefficients(usize n) 
        : n_{2u}, F_{Value::raw(1), Value::raw(1)}, inv_{Value{}, Value::raw(1)}, 
        invF_{Value::raw(1), Value::raw(1)} {
        assert(n);
        if (need(n)) expand(n);
    }
    Value F(i32 n) {
        if (need(std::abs(n))) expand(static_cast<usize>(std::abs(n)));
        return (n >= 0 ? F_[n] : invF_[-n]);
    }
    Value P(i32 p, i32 q) {
        if (q > p) return Value{}; 
        return F(p) * F(q - p);
    }
    Value C(i32 p, i32 q) {
        if (q > p) return Value{};
        return P(p, q) * F(-q);
    }
    Value H(i32 p, i32 q) {
        if (p == 0 and q == 0) return Value::raw(1);
        return C(p + q - 1, q);
    }
    Value B(const std::vector<i32>& b) {
        Value res{1};
        i32 sum{};
        for (i32 x : b) {
            res *= F(-x);
            sum += x;
        }
        res *= F(sum);
        return res;
    }
};

} // namespace zawa
#line 5 "117.test.cpp"

#line 7 "117.test.cpp"
#include <tuple>
using namespace zawa;

/*
 * yukicoder No.117 組み合わせの数
 */

std::tuple<char, int, int> parse() {
    std::string s; std::cin >> s;
    char c{s[0]};
    int i{2};
    int p{};
    for (; s[i] != ',' ; i++) {
        p = (p * 10) + (s[i] - '0');
    }
    int q{};
    for (i++ ; s[i] != ')' ; i++) {
        q = (q * 10) + (s[i] - '0');
    }
    return { c, p, q };
}

void solve() {
    int t; std::cin >> t;
    BinomalCoefficients<1000000007> comb(1000000);
    for (int _{} ; _ < t ; _++) {
        auto [c, p, q]{parse()}; 
        if (c == 'C') std::cout << comb.C(p, q).val() << '\n';
        else if (c == 'P') std::cout << comb.P(p, q).val() << '\n';
        else std::cout << comb.H(p, q).val() << '\n';
    }
}

int main() {
#ifdef ONLINE_JUDGE
    solve();
#else
    std::cout << "Hello World" << '\n';
#endif
}
0