結果

問題 No.117 組み合わせの数
ユーザー 👑 emthrmemthrm
提出日時 2019-02-18 16:46:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 202 ms / 5,000 ms
コード長 2,312 bytes
コンパイル時間 1,190 ms
コンパイル使用メモリ 124,992 KB
実行使用メモリ 159,672 KB
最終ジャッジ日時 2023-07-29 04:27:11
合計ジャッジ時間 2,100 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
const int MOD = 1000000007;

pair<long long, long long> ext_gcd(long long a, long long b) {
  if (b == 0) return {1, 0};
  pair<long long, long long> pr = ext_gcd(b, a % b);
  return {pr.second, pr.first - a / b * pr.second};
}

long long mod_inv(long long a) {
  if (__gcd(a, (long long)MOD) != 1) return 0;
  pair<long long, long long> pr = ext_gcd(a, MOD);
  return (pr.first + MOD) % MOD;
}

const int MAX = 10000000;
long long fact[MAX + 1], fact_inv[MAX + 1];
void nCk_init(int num = MAX) {
  fact[0] = 1;
  FOR(i, 1, MAX + 1) fact[i] = fact[i - 1] * i % MOD;
  fact_inv[MAX] = mod_inv(fact[MAX]);
  for (int i = MAX; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i % MOD;
}

inline long long nCk(int n, int k) {
  if (n < 0 || n < k || k < 0) return 0;
  return fact[n] * (fact_inv[k] * fact_inv[n - k] % MOD) % MOD;
}

inline long long nPk(int n, int k) {
  if (n < 0 || n < k || k < 0) return 0;
  return fact[n] * fact_inv[n - k] % MOD;
}

inline long long nHk(int n, int k) {
  if (n < 0 || k < 0) return 0;
  return (k == 0 ? 1 : nCk(n + k - 1, k));
}

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  nCk_init(1000000);
  int t; cin >> t;
  while (t--) {
    string s; cin >> s;
    string n = "";
    int idx = 2;
    for (;; ++idx) {
      char c = s[idx];
      if (c == ',') break;
      n += c;
    }
    string k = s.substr(idx + 1);
    if (s[0] == 'C') cout << nCk(stoi(n), stoi(k));
    else if (s[0] == 'P') cout << nPk(stoi(n), stoi(k));
    else cout << nHk(stoi(n), stoi(k));
    cout << '\n';
  }
  return 0;
}
0