結果

問題 No.117 組み合わせの数
ユーザー kyort0nkyort0n
提出日時 2019-08-27 03:02:16
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 5,000 ms
コード長 1,988 bytes
コンパイル時間 1,464 ms
コンパイル使用メモリ 166,300 KB
実行使用メモリ 237,800 KB
最終ジャッジ日時 2023-08-08 23:26:59
合計ジャッジ時間 2,657 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
const ll mod = 1000000007;
ll inv[10000100];
ll FactorialInv[10000100];
ll Factorial[10000100];
ll beki(ll a, ll b){
    if(b == 0){
        return 1;
    }
    ll ans = beki(a, b / 2);
    ans = ans * ans % mod;
    if(b % 2 == 1){
        ans = ans * a % mod;
    }
    return ans;
}
void init_combination(){
    const int MAX = 10000002;
    Factorial[0] = 1;
    inv[0] = 1;
    for(int i = 1; i <= MAX; i++){
        Factorial[i] = Factorial[i - 1] * i % mod;
    }
    FactorialInv[MAX] = beki(Factorial[MAX], mod - 2);
    for(ll i = MAX - 1; i >= 0; i--) {
        FactorialInv[i] = FactorialInv[i+1] * (i+1) % mod;
    }
    for(int i = 1; i <= MAX; i++) {
        inv[i] = FactorialInv[i] * Factorial[i-1] % mod;
    }
}
ll combination(ll a, ll b){
    if((a == b) || (b == 0)){
        return 1;
    }
    if(a < b) return 0;
    ll ans = Factorial[a] * FactorialInv[b] % mod;
    ans = ans * FactorialInv[a - b] % mod;
    return ans;
}

void C(ll n, ll k) {
    cout << combination(n, k) << "\n";
}

void P(ll n, ll k) {
    cout << combination(n, k) * Factorial[k] % mod << "\n";
}

void H(ll n, ll k) {
    cout << combination(n + k - 1, k) << "\n";
}

void solve() {
    char in;
    cin >> in;
    char dust;
    cin >> dust;
    ll n, k;
    cin >> n >> dust >> k >> dust;
    if(in == 'C') C(n, k);
    if(in == 'P') P(n, k);
    if(in == 'H') H(n, k);
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    init_combination();
    ll T;
    cin >> T;
    while(T--) solve();
    return 0;
}
0