結果

問題 No.117 組み合わせの数
コンテスト
ユーザー lloyz
提出日時 2023-07-01 23:21:59
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 295 ms / 5,000 ms
コード長 776 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 225 ms
コンパイル使用メモリ 85,248 KB
実行使用メモリ 128,528 KB
最終ジャッジ日時 2026-03-30 02:23:53
合計ジャッジ時間 3,217 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

mod = 10**9 + 7
fact = [1] * (2 * 10**6 + 1)
inv = [1] * (2 * 10**6 + 1)
finv = [1] * (2 * 10**6 + 1)
for i in range(2, 2 * 10**6 + 1):
    fact[i] = fact[i - 1] * i % mod
    inv[i] = mod - inv[mod % i] * (mod // i) % mod
    finv[i] = finv[i - 1] * inv[i] % mod
def c(x, y):
    if x < y:
        return 0
    if y == 0:
        return 1
    return fact[x] * finv[y] % mod * finv[x - y] % mod

def p(x, y):
    return c(x, y) * fact[y] % mod

def h(x, y):
    if x == y == 0:
        return 1
    return c(x + y - 1, y)

for _ in range(int(input())):
    s = input()
    n = len(s)
    s1, s2 = s[0], s[2:n - 1]
    x, y = map(int, s2.split(','))
    if s1 == 'P':
        print(p(x, y))
    elif s1 == 'C':
        print(c(x, y))
    elif s1 == 'H':
        print(h(x, y))
0