結果

問題 No.117 組み合わせの数
ユーザー codershifthcodershifth
提出日時 2015-08-13 01:42:25
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,181 bytes
コンパイル時間 1,250 ms
コンパイル使用メモリ 150,068 KB
実行使用メモリ 26,632 KB
最終ジャッジ日時 2023-09-25 11:21:04
合計ジャッジ時間 2,841 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

class nCombP {
public:
    long long ModP;
    std::vector<long long> fact_;
    std::vector<long long> factr_;

    nCombP(int maxN, long long P) : ModP(P) {
            std::vector<long long> inv(maxN+1, 0);
            fact_.resize(maxN+1,0);
            factr_.resize(maxN+1,0);
            inv[1] = fact_[0] = factr_[0] = 1;
            for (int i = 2; i <= maxN; ++i)
                inv[i] = inv[ModP % i] * (ModP - ModP/i) % ModP;
            for (int i = 1; i <= maxN; ++i)
            {
                fact_[i]  = fact_[i-1]*i % ModP;
                factr_[i] = factr_[i-1]*inv[i] % ModP;
            }
    }
    long long operator()(long long n, long long r) {
            if (r < 0 || r > n)
                return 0;
            return (fact_[n] * factr_[r] % ModP) * factr_[n-r] % ModP;
    }
};
class CombinationNumber {
public:
    void solve(void) {
            const int Mod = (int)(1e+9)+7;
            const int maxN = (int)1e+6;

            // O(maxN)
            nCombP nCr(maxN,Mod);

            int T;
            cin>>T;
            REP(_, T)
            {
                char type, a,b,c;
                int n,k;
                cin>>type>>a>>n>>b>>k>>a;
                (void)a;
                (void)b;
                (void)c;
                switch (type)
                {
                  case 'C':
                    cout<<nCr(n,k)<<endl;
                    break;
                  case 'P':
                    cout<<(nCr.fact_[k]*nCr(n,k))%Mod<<endl;
                    break;
                  case 'H':
                    // nHk = (n+k-1)Ck
                    cout<<nCr(n+k-1,k)<<endl;
                    break;
                  default:
                    assert(false);
                }
            }
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new CombinationNumber();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0