結果

問題 No.117 組み合わせの数
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2022-12-05 05:32:19
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 286 ms / 5,000 ms
コード長 1,436 bytes
コンパイル時間 1,314 ms
コンパイル使用メモリ 140,144 KB
実行使用メモリ 34,420 KB
最終ジャッジ日時 2024-04-20 08:47:30
合計ジャッジ時間 2,305 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>

using namespace std;

const long long modc=1e9+7, MAX=2000000;
vector<long long> f, finv;

long long inv(long long x){
    long long ans = 1, e = modc-2;
    x %= modc;

    while (e > 0){
        if ((e & 1LL)) ans = (ans * x) % modc;
        e = e >> 1LL;
        x = (x*x) % modc;
    }

    return ans;
}


void init(){
    f.resize(MAX+1); finv.resize(MAX+1);
    f[0] = 1;
    for (int i=1; i<=MAX; i++) f[i] = (f[i-1]*i) % modc;
    finv[MAX] = inv(f[MAX]);
    for (int i=MAX-1; i>=0; i--) finv[i] = (finv[i+1] * (i+1)) % modc;
}

long long C(long long n, long long k){ 
    if (n < k || k < 0) return 0;

    return (((f[n] * finv[k]) % modc) * finv[n-k]) % modc;
}

long long P(long long n, long long k){
    if (n < k || k < 0) return 0;

    return (f[n] * finv[n-k]) % modc;
}

long long H(long long n, long long k){
    if (n == 0 && k == 0) return 1;
    return C(n+k-1, k);
}

int main(){

    init();

    int T;
    cin >> T;
    for (int i=0; i<T; i++) {
        string s; cin >> s;
        char c; int a, b;
        sscanf(s.c_str(), "%c(%d,%d)", &c, &a, &b);

        int ans;
        if (c == 'C') ans = C(a, b);
        else if (c == 'P') ans = P(a, b);
        else ans = H(a, b);
        cout << ans << endl;
    }

    return 0;
}
0