結果

問題 No.117 組み合わせの数
ユーザー bad_boy_gaogaobad_boy_gaogao
提出日時 2021-08-02 23:58:38
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,604 bytes
コンパイル時間 1,884 ms
コンパイル使用メモリ 202,144 KB
実行使用メモリ 26,484 KB
最終ジャッジ日時 2023-10-14 21:00:13
合計ジャッジ時間 2,995 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template<class T=ll> using vec = vector<T>;
#define BE(x) x.begin(), x.end()

const ll max_n = 1000000;
vec<ll> inv(max_n+1,1), fact(max_n+1,1), finv(max_n+1,1);
bool pre = true;

ll comb (ll n, ll k, ll m = 100000007) {
    if (pre) {
        for (ll i = 2; i <= max_n; i++) {
            inv [i] = m-inv[m%i]*(m/i)%m;    // inverse for i
            fact[i] = fact[i-1]*i%m;         // i!
            finv[i] = finv[i-1]*inv[i]%m;    // inverse for i!
        }
        pre = false;
    }
    if (n<k || n<0 || k<0)
        return 0;
    else
        return fact[n]*(finv[k]*finv[n-k]%m)%m;
}   // set max_n value
    // n and k are up to about 10^7
    // pre:O(max_n), query:O(1)

int main() {
    const ll mod = 1000000007;
    int q;
    cin >> q;
    for (int _ = 0; _ < q; _++) {

        string s;
        cin >> s;
        char c = s[0];
        string sn, sk;
        s.erase(0,2);
        bool f = false;
        while (true) {
            if (!f) {
                if (s[0] == ',') f = true;
                else sn += s[0];
                s.erase(0,1);
            }
            else {
                if (s[0] == ')') break;
                sk += s[0];
                s.erase(0,1);
            }
        }
        ll n = stoll(sn), k = stoll(sk);

        ll ans;
        if (c == 'C') {
            ans = comb(n, k, mod);
        }
        else if (c == 'P') {
            ans = comb(n, k, mod)*fact[k]%mod;
        }
        else {
            ans = comb(n+k-1, k, mod);
        }
        cout << ans << '\n';
    }
}
0