結果

問題 No.117 組み合わせの数
ユーザー maspymaspy
提出日時 2020-03-06 14:10:51
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,525 ms / 5,000 ms
コード長 1,522 bytes
コンパイル時間 99 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 230,244 KB
最終ジャッジ日時 2024-04-22 03:53:20
合計ジャッジ時間 4,075 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
import numpy as np

MOD = 10**9 + 7

# %%


def cumprod(A, MOD=MOD):
    L = len(A)
    Lsq = int(L**.5 + 1)
    A = np.resize(A, Lsq**2).reshape(Lsq, Lsq)
    for n in range(1, Lsq):
        A[:, n] *= A[:, n - 1]
        A[:, n] %= MOD
    for n in range(1, Lsq):
        A[n] *= A[n - 1, -1]
        A[n] %= MOD
    return A.ravel()[:L]


def make_fact(U, MOD=MOD):
    x = np.arange(U, dtype=np.int64)
    x[0] = 1
    fact = cumprod(x, MOD)
    x = np.arange(U, 0, -1, dtype=np.int64)
    x[0] = pow(int(fact[-1]), MOD - 2, MOD)
    fact_inv = cumprod(x, MOD)[::-1]
    fact.flags.writeable = False
    fact_inv.flags.writeable = False
    return fact, fact_inv


# %%
fact, fact_inv = make_fact(2 * 10 ** 6 + 100)
fact = fact.tolist()
fact_inv = fact_inv.tolist()


# %%
def solve(S):
    S = S.replace('(', ',').replace(')', '').split(',')
    N, K = map(int, S[1:])
    if S[0] == 'C':
        if K > N:
            return 0
        return fact[N] * fact_inv[K] * fact_inv[N - K] % MOD
    if S[0] == 'P':
        if K > N:
            return 0
        return fact[N] * fact_inv[N - K] % MOD
    if S[0] == 'H':
        if not N:
            if not K:
                return 1
            else:
                return 0
        return fact[N + K - 1] * fact_inv[K] * fact_inv[N - 1] % MOD


# %%
T = int(readline())
for q in read().decode().split():
    print(solve(q))
0