結果

問題 No.117 組み合わせの数
ユーザー creep04creep04
提出日時 2018-03-15 13:42:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 228 ms / 5,000 ms
コード長 705 bytes
コンパイル時間 1,357 ms
コンパイル使用メモリ 145,420 KB
実行使用メモリ 19,092 KB
最終ジャッジ日時 2023-08-22 12:20:09
合計ジャッジ時間 2,423 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;

int t;
char c[100];
ll n, k, f[2002002];

ll mpow(ll x, ll n = mod-2, ll m = mod) {
	ll res = 1;
	while (n>0) {
		if (n&1) res = res * x %m;
		x = x * x %m;
		n >>= 1;
	}
	return res;
}

ll comb(ll n, ll k) {
	if (n==-1 && k==0) return 1;
	if (n<k) return 0;
	return f[n] * mpow(f[k]) %mod * mpow(f[n-k]) %mod;
}

int main() {
	f[0] = 1;
	for (ll i = 1; i <= 2000000; ++i) f[i] = f[i-1]*i %mod;
	scanf("%d", &t);
	while (t--) {
		scanf("%1s(%lld,%lld)", c, &n, &k);
		if (c[0]=='C') cout << comb(n,k) << endl;
		else if (c[0]=='P') cout << comb(n,k) * f[k] %mod << endl;
		else cout << comb(n+k-1,k) << endl;
	}
}
0