結果

問題 No.117 組み合わせの数
ユーザー kcm1700kcm1700
提出日時 2015-01-04 23:39:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 1,225 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 49,224 KB
実行使用メモリ 26,352 KB
最終ジャッジ日時 2023-09-03 21:43:35
合計ジャッジ時間 953 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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