結果

問題 No.117 組み合わせの数
ユーザー なおなお
提出日時 2015-01-05 00:10:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 277 ms / 5,000 ms
コード長 1,266 bytes
コンパイル時間 1,163 ms
コンパイル使用メモリ 145,464 KB
実行使用メモリ 19,008 KB
最終ジャッジ日時 2023-09-03 21:52:14
合計ジャッジ時間 2,149 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))

int T;
ll fact[2000001];
const int MOD = 1000000000+7;

void init(){
    fact[0] = fact[1] = 1;
    for(int i = 2; i <= 2000000; i++){
        fact[i] = (fact[i-1] * i) % MOD;
    }
}

ll extgcd(ll a,ll b,ll &m,ll &n){ll g=a;m=1;n=0;if(b)g=extgcd(b,a%b,n,m),n-=(a/b)*m;return g;}
ll divmod(ll n, ll m, ll mod){
    ll a,b;
    extgcd(m,mod,a,b);
    return (n * a) % mod;
}

int c(int n, int r){
    if(n < r) return 0;
    return divmod(fact[n], (fact[r] * fact[n-r])%MOD, MOD);
}
int p(int n, int r){
    if(n < r) return 0;
    return divmod(fact[n], fact[n-r], MOD);
}
int h(int n, int r){
    if(n == 0 && r == 0) return 1;
    return c(n+r-1,r);
}

int main(){
    init();
    cin >> T;
    string s;
    REP(i,T){
        int n,m,res = 0;
        cin >> s;
        if(sscanf(s.c_str(), "C(%d,%d)", &n, &m) == 2){
            res = c(n,m);
        }
        if(sscanf(s.c_str(), "P(%d,%d)", &n, &m) == 2){
            res = p(n,m);
        }
        if(sscanf(s.c_str(), "H(%d,%d)", &n, &m) == 2){
            res = h(n,m);
        }
        cout << ((res + MOD) % MOD) << endl;
    }
}
0