結果

問題 No.117 組み合わせの数
ユーザー mayoko_mayoko_
提出日時 2015-01-05 00:11:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 286 ms / 5,000 ms
コード長 1,977 bytes
コンパイル時間 687 ms
コンパイル使用メモリ 76,964 KB
実行使用メモリ 19,000 KB
最終ジャッジ日時 2023-09-03 21:52:22
合計ジャッジ時間 1,566 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

#define INF 1000000000

using namespace std;
typedef long long ll;
const ll MAX = 2000010;
const ll MOD = 1000000007;
ll amari[MAX];

// extgcd
ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}

// mod_inverse
ll mod_inverse(ll a, ll m) {
    ll x, y;
    extgcd(a, m, x, y);
    return (m+x%m) % m;
}

void init() {
    amari[0] = 1;
    amari[1] = 1;
    for (ll i = 2; i < MAX; i++) {
        amari[i] = (amari[i-1] * i) % MOD;
    }
}

int main(void) {
    int T;
    cin >> T;
    init();
    while (T--) {
        string s;
        cin >> s;
        string a, b;
        int tmp = 0;
        while (s[tmp] != ',') tmp++;
        a = s.substr(2, tmp-2);
        b = s.substr(tmp+1, s.size()-tmp-2);
        ll N = stoll(a);
        ll K = stoll(b);
        if (s[0] == 'P') {
            if (N < K) {
                cout << 0 << endl;
                continue;
            }
            cout << (amari[N] * mod_inverse(amari[N-K], MOD)) % MOD << endl;
        } else if (s[0] == 'C') {
            if (N < K) {
                cout << 0 << endl;
                continue;
            }
            ll first = (amari[N] * mod_inverse(amari[K], MOD)) % MOD;
            cout << (first * mod_inverse(amari[N-K], MOD)) % MOD << endl;
        } else if (s[0] == 'H') {
            if (N == 0 && K == 0) {
                cout << 1 << endl;
                continue;
            }
            ll NN = N+K-1;
            ll first = (amari[NN] * mod_inverse(amari[K], MOD)) % MOD;
            cout << (first * mod_inverse(amari[NN-K], MOD)) % MOD << endl;
        }
    }
    return 0;
}
0