結果

問題 No.117 組み合わせの数
ユーザー 0w10w1
提出日時 2016-11-28 13:27:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 540 ms / 5,000 ms
コード長 1,471 bytes
コンパイル時間 1,777 ms
コンパイル使用メモリ 167,488 KB
実行使用メモリ 19,036 KB
最終ジャッジ日時 2023-08-18 08:45:23
合計ジャッジ時間 3,348 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MOD7 = ( int ) 1e9 + 7;
const int MAXN = ( int ) 1e6;
const int MAXK = ( int ) 1e6;

int fact[ MAXN + MAXK ], inv_fact[ MAXN + MAXK ];

int int_pow( int x, int p ){
  int res = 1;
  while( p ){
    if( p & 1 )
      res = 1LL * res * x % MOD7;
    p >>= 1;
    x = 1LL * x * x % MOD7;
  }
  return res;
}

int inv( int x ){
  return int_pow( x, MOD7 - 2 );
}

signed main(){
  for( int i = fact[ 0 ] = inv_fact[ 0 ] = 1; i < MAXN + MAXK; ++i )
    fact[ i ] = 1LL * fact[ i - 1 ] * i % MOD7,
    inv_fact[ i ] = 1LL * inv_fact[ i - 1 ] * inv( i ) % MOD7;
  int T; cin >> T;
  for( int kase = 0; kase < T; ++kase ){
    string S; cin >> S;
    char type = S[ 0 ];
    int N = 0, K = 0;
    for( int i = 2; i < S.size(); ++i ){
      if( S[ i ] == ',' ){
        for( int j = i + 1; S[ j ] != ')'; ++j )
          K = K * 10 + S[ j ] - '0';
        break;
      }
      N = N * 10 + S[ i ] - '0';
    }
    if( N == 0 and K == 0 )
      cout << 1 << endl;
    else if( N == 0 )
      cout << 0 << endl;
    else if( ( type == 'C' or type == 'P' ) and N < K )
      cout << 0 << endl;
    else if( type == 'C' )
      cout << 1LL * fact[ N ] * inv_fact[ K ] % MOD7 * inv_fact[ N - K ] % MOD7 << endl;
    else if( type == 'P' )
      cout << 1LL * fact[ N ] * inv_fact[ N - K ] % MOD7 << endl;
    else
      cout << 1LL * fact[ N + K - 1 ] * inv_fact[ K ] % MOD7 * inv_fact[ N - 1 ] % MOD7 << endl;
  }
  return 0;
}
0