結果
問題 | No.117 組み合わせの数 |
ユーザー |
![]() |
提出日時 | 2020-03-06 14:10:51 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 1,497 ms / 5,000 ms |
コード長 | 1,522 bytes |
コンパイル時間 | 94 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 230,624 KB |
最終ジャッジ日時 | 2024-10-14 02:47:16 |
合計ジャッジ時間 | 3,509 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 1 |
ソースコード
#!/usr/bin/env python3# %%import sysread = sys.stdin.buffer.readreadline = sys.stdin.buffer.readlinereadlines = sys.stdin.buffer.readlinesimport numpy as npMOD = 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] %= MODfor n in range(1, Lsq):A[n] *= A[n - 1, -1]A[n] %= MODreturn A.ravel()[:L]def make_fact(U, MOD=MOD):x = np.arange(U, dtype=np.int64)x[0] = 1fact = 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 = Falsefact_inv.flags.writeable = Falsereturn 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 0return fact[N] * fact_inv[K] * fact_inv[N - K] % MODif S[0] == 'P':if K > N:return 0return fact[N] * fact_inv[N - K] % MODif S[0] == 'H':if not N:if not K:return 1else:return 0return fact[N + K - 1] * fact_inv[K] * fact_inv[N - 1] % MOD# %%T = int(readline())for q in read().decode().split():print(solve(q))