結果

問題 No.117 組み合わせの数
ユーザー codershifthcodershifth
提出日時 2015-08-13 01:53:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 332 ms / 5,000 ms
コード長 2,465 bytes
コンパイル時間 1,326 ms
コンパイル使用メモリ 150,172 KB
実行使用メモリ 50,064 KB
最終ジャッジ日時 2023-09-25 11:21:19
合計ジャッジ時間 2,401 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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(2*maxN,Mod); // nCr(n+k-1,k をやるので 2*maxN 確保
            auto &fact = nCr.fact_; // fact[n] = n!

            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<<(fact[k]*nCr(n,k))%Mod<<endl;
                    break;
                  case 'H':
                    // nHk = (n+k-1)Ck
                    // H(0,0) = C(-1,0) = 1 がコーナーケースなので対応
                    if (n==0 && k==0)
                        cout<<1<<endl;
                    else
                        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