結果

問題 No.117 組み合わせの数
ユーザー YutaroYamanakaYutaroYamanaka
提出日時 2019-07-04 11:43:58
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 1,469 bytes
コンパイル時間 608 ms
コンパイル使用メモリ 68,404 KB
実行使用メモリ 21,164 KB
最終ジャッジ日時 2023-10-19 07:34:31
合計ジャッジ時間 2,686 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:58:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   58 |         scanf(" %c(%d,%d)",&c[i] ,&a[i] ,&b[i]);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;

int T;
const int MAX = 1000000;
const ll MOD = pow(10,9)+7;
vector<ll> fct(1000001);
vector<ll> invfct(1000001);

ll mpow(ll x, ll n){ //x^n(mod M)
    ll ans = 1;
    while(n != 0){
        if(n&1) ans = ans*x % MOD;
        x = x*x % MOD;
        n = n >> 1;
    }
    return ans;
}

ll comb(ll a, ll b){ //aCb(mod M)
    if(a == 0 && b == 0)return 1;
    if(a < b || a < 0)return 0;
    ll tmp = invfct[a-b]* invfct[b] % MOD;
    return tmp * fct[a] % MOD;
}

ll perm(ll a, ll b){
    if(a < b) return 0;
    ll tmp = invfct[a-b] % MOD;
    return tmp * fct[a] % MOD;
}

ll dupc(ll a,ll b){
    if(a == 0 && b == 0)return 1;
    if(a < 0 || b == 0)return 0;
    ll tmp = invfct[a-1]* invfct[b] % MOD;
    return tmp * fct[a+b-1] % MOD;
}

int main(void){
    // Your code here!
    
    fct[0] = 1;
    invfct[0] = 1;
    for(ll i = 0; i < 1000000; i++){
        fct[i+1] = fct[i] * (i+1) % MOD;
        invfct[i+1] = invfct[i] * mpow(i+1, MOD-2) % MOD;
    }


    cin >> T;
    vector<char> c(T);
    vector<int> a(T),b(T);
    for(int i = 0;  i < T; i++){
        scanf(" %c(%d,%d)",&c[i] ,&a[i] ,&b[i]);
    }
    
    for(int i  =0 ; i < T; i++){
        ll ans = 0;
        if(c[i] == 'P') ans = perm(a[i], b[i]);
        if(c[i] == 'H') ans = dupc(a[i], b[i]);
        if(c[i] == 'C') ans = comb(a[i], b[i]);
        cout << ans << endl;
    }
    return 0;
}
0