結果

問題 No.117 組み合わせの数
ユーザー ynayna
提出日時 2019-07-23 21:01:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,455 bytes
コンパイル時間 1,774 ms
コンパイル使用メモリ 172,684 KB
実行使用メモリ 20,024 KB
最終ジャッジ日時 2024-06-26 08:46:42
合計ジャッジ時間 2,708 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

#ifndef MOD_INT
#define MOD_INT
template<int mod>
struct ModInt{
  int x;

  ModInt() : x(0){}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod){}

  ModInt &operator+=(const ModInt &p){
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p){
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p){
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p){
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const {return ModInt(-x);}
  ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;}
  ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;}
  ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;}
  ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;}
  bool operator==(const ModInt &p) const {return x == p.x;}
  bool operator!=(const ModInt &p) const {return x != p.x;}

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p){
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a){
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod(){return mod;}
};

using mint = ModInt<MOD>;
#endif
#ifndef MOD_INT
#define MOD_INT
template<int mod>
struct ModInt{
  int x;

  ModInt() : x(0){}
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod){}

  ModInt &operator+=(const ModInt &p){
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &p){
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &p){
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p){
    *this *= p.inverse();
    return *this;
  }

  ModInt operator-() const {return ModInt(-x);}
  ModInt operator+(const ModInt &p) const {return ModInt(*this) += p;}
  ModInt operator-(const ModInt &p) const {return ModInt(*this) -= p;}
  ModInt operator*(const ModInt &p) const {return ModInt(*this) *= p;}
  ModInt operator/(const ModInt &p) const {return ModInt(*this) /= p;}
  bool operator==(const ModInt &p) const {return x == p.x;}
  bool operator!=(const ModInt &p) const {return x != p.x;}

  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }

  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }

  friend ostream &operator<<(ostream &os, const ModInt &p){
    return os << p.x;
  }

  friend istream &operator>>(istream &is, ModInt &a){
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }

  static int get_mod(){return mod;}
};

using mint = ModInt<MOD>;
#endif

class Factorial{
    private:
        int max;
        vector<mint> fact, ifact;

        void resize(int n){
            if(n <= max) return;

            fact.resize(n+1);
            ifact.resize(n+1);
            for(int i = max + 1; i <= n; i++) fact[i] = fact[i-1] * i;
            ifact[n] = fact[n].inverse();
            for(int i = n; i >= max + 1; i--) ifact[i-1] = ifact[i] * i;
            max = n;
        }

    public:
        Factorial(int N = 0):max(N), fact(N+1), ifact(N+1){
            fact[0] = 1;
            for(int i = 1; i <= N; i++) fact[i] = fact[i-1] * i;
            ifact[N] = fact[N].inverse();
            for(int i = N; i >= 1; i--) ifact[i-1] = ifact[i] * i;
        }

        mint operator()(int n){
            if(n > max) this -> resize(n);

            return fact[n];
        }

        mint inverse(int n){
            if(n > max) this -> resize(n);

            return ifact[n];
        }

        mint combination(int n, int k){
            if(k < 0 or n < k) return 0;
            if(n > max) this -> resize(n);

            return fact[n]*ifact[k]*ifact[n-k];
        }

        mint permutation(int n, int k){
            if(k < 0 or n < k) return 0;
            if(n > max) this -> resize(n);

            return fact[n]*ifact[n-k];
        }

        mint hcombination(int n, int k){
            return this -> combination(n+k-1, k);
        }
};

int main(void){
    int T;
    cin >> T;

    Factorial fact;
    vector<mint> vec;

    for(int i = 0; i < T; i++){
        char c, buf;
        int n, k;
        cin >> c >> buf >> n >> buf >> k >> buf;

        if(c == 'C'){
            vec.push_back(fact.combination(n, k));
        }else if(c == 'P'){
            vec.push_back(fact.permutation(n, k));
        }else if(c == 'H')
        {
            vec.push_back(fact.hcombination(n, k));
        }
    }

    for(int i = 0; i < T; i++){
        cout << vec[i] << '\n';
    }

    return 0;
}
0