結果

問題 No.117 組み合わせの数
ユーザー bombrary_skbombrary_sk
提出日時 2019-05-05 11:47:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 225 ms / 5,000 ms
コード長 2,373 bytes
コンパイル時間 1,037 ms
コンパイル使用メモリ 106,856 KB
実行使用メモリ 18,996 KB
最終ジャッジ日時 2023-09-06 19:00:46
合計ジャッジ時間 1,873 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

#define REP(i,n) for(ll (i) = (0); (i) < (n); ++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define ALL(v) v.begin(),v.end()
#define Decimal fixed << setprecision(20)
#define SHOWP(x) cerr<<"["<<(x).FI<<", "<<(x).SE<<"]";
#define SHOWX(x) cerr<<#x<<": "<<x<<endl;
#define SHOWVEC(v, e) REP(i, e) cerr << (v[i]) << ' '; cerr << endl;
#define SHOW2D(a, h, w) REP(i, h){REP(j, w)cerr<<setw(3)<<(a[i][j])<<' ';cerr<<endl;}
constexpr int INF = 1 << 30 - 1;
constexpr long long LLINF = 1LL << 60;
constexpr long long MOD = 1000000007;

typedef long long ll;
typedef pair<ll, ll> P;
typedef string::const_iterator State;

int number(State &s) {
  int ret = 0;
  while (isdigit(*s)) {
    ret *= 10;
    ret += *s - '0';
    s++;
  }
  return ret;
}

pair<char, pair<int, int>> parse(State &s) {
  pair<char, pair<int, int>> ret;
  ret.first = *s++;
  s++;
  ret.second.first = number(s);
  s++;
  ret.second.second = number(s);
  return ret;
}

long long modpow(long long a, long long n)
{
  long long ret = 1;
  while (n > 0) {
    if (n % 2 == 1) ret *= a;
    ret %= MOD;
    a = a*a % MOD;
    n /= 2;
  }
  return ret;
}

long long moddiv(long long a, long long b) {
  return (a * modpow(b, MOD - 2)) % MOD;
}

#define MAX_N 2000000
long long fact[MAX_N];
void factInit() {
  fact[0] = 1;
  for (int i = 1; i < MAX_N; i++) {
    fact[i] = (i * fact[i - 1]) % MOD;
  }
}
long long comb(int n, int r) {
  if (n < r || n < 0 || r < 0) return 0;
  return moddiv(fact[n], (fact[r] * fact[n - r]) % MOD);
}
long long perm(int n, int r) {
  if (n < r || n < 0 || r < 0) return 0;
  return moddiv(fact[n], fact[n - r]);
}
long long homo(int n, int r) {
	if (n == 0 && r == 0) return 1;
  return comb(n + r - 1, r);
}

int main()
{
  factInit();
  int T;
  cin >> T;
  for (int i = 0; i < T; i++) {
    string S;
    cin >> S;
    State begin = S.begin();
    pair<char, pair<int, int>> tmp = parse(begin);
    int c = tmp.first;
    int n = tmp.second.first;
    int r = tmp.second.second;
    if (c == 'C') cout << comb(n, r) << endl;
    if (c == 'P') cout << perm(n, r) << endl;
    if (c == 'H') cout << homo(n, r) << endl;
  }
  return 0;
}
0