結果

問題 No.117 組み合わせの数
ユーザー face4face4
提出日時 2018-10-08 19:07:49
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,711 ms / 5,000 ms
コード長 1,403 bytes
コンパイル時間 533 ms
コンパイル使用メモリ 64,884 KB
実行使用メモリ 34,944 KB
最終ジャッジ日時 2024-04-20 18:39:37
合計ジャッジ時間 4,456 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
using namespace std;

typedef long long ll;
const ll mod = 1e9 + 7;

ll modpow(ll a, ll b, ll p = 1e9+7){
    if(b == 0)  return 1;

    if(b % 2 == 0){
        ll d = modpow(a, b/2, p);
        return (d*d) % p;
    }else{
        return (a%p * modpow(a, b-1, p)) % p;
    }
}

#define N 2000005
ll po[N];
ll inv[N];

int main(){
    po[0] = 1;
    for(int i = 1; i < N; i++)  po[i] = (po[i-1] * i) % mod;
    inv[0] = 1;
    for(int i = 1; i < N; i++)  inv[i] = modpow(po[i], mod-2, mod);

    int t;
    scanf("%d\n", &t);

    char c;
    int n, k;
    for(int i = 0; i < t; i++){
        ll ans = 1;
        scanf("%c(%d,%d)\n", &c, &n, &k);

        if(c != 'H' && n < k){
            printf("0\n");
            continue;
        }

        if(c == 'H' && n == 0 && k == 0){
            printf("1\n");
            continue;
        }
        
        switch(c){
            case 'C':
            ans = (ans * po[n]) % mod;
            ans = (ans * inv[n-k]) % mod;
            ans = (ans * inv[k]) % mod;
            break;
            case 'P':
            ans = (ans * po[n]) % mod;
            ans = (ans * inv[n-k]) % mod;
            break;
            case 'H':
            ans = (ans * po[n+k-1]) % mod;
            ans = (ans * inv[k]) % mod;
            ans = (ans * inv[n+k-1-k]) % mod;
            break;
        }

        printf("%d\n", ans);
    }

    return 0;
}
0