結果

問題 No.117 組み合わせの数
コンテスト
ユーザー kcm1700
提出日時 2015-01-04 23:39:33
言語 C++11
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=gnu++11 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 64 ms / 5,000 ms
コード長 1,225 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 470 ms
コンパイル使用メモリ 67,980 KB
実行使用メモリ 27,236 KB
最終ジャッジ日時 2026-03-05 21:45:41
合計ジャッジ時間 690 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

const int mod = 1000000007;
int inv[2000003];
int fact[2000003];
int ifact[2000003];

int main()
{
  int T;
  inv[1] = 1;
  for (int i = 2; i <= 2000000; i++) {
    inv[i] = (long long)(mod - mod / i) * inv[mod%i] % mod;
  }
  fact[0] = 1;
  for (int i = 1; i <= 2000000; i++) fact[i] = fact[i-1] * (long long)i % mod;
  ifact[0] = 1;
  for (int i = 1; i <= 2000000; i++) ifact[i] = ifact[i-1] * (long long)inv[i] % mod;
  scanf("%d",&T);
  while(T-->0){
    char front[30];
    int n,k;
    scanf("%2s%d,%d%*s",front, &n,&k);
    long long ans = 0;
    if (front[0] == 'C') {
      if (n < k) ans = 0;
      else {
        ans = fact[n];
        ans *= ifact[k]; ans %= mod;
        ans *= ifact[n-k]; ans %= mod;
      }
    } else if (front[0] == 'P') {
      if (n-k < 0) ans = 0;
      else {
        ans = fact[n];
        ans *= ifact[n-k]; ans %= mod;
      }
    } else if (front[0] == 'H') {
      if (n == 0 && k == 0) {
        ans = 1;
      } else {
        ans = fact[n+k-1];
        ans *= ifact[k]; ans %= mod;
        ans *= ifact[n-1]; ans %= mod;
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
0