結果
問題 |
No.389 ロジックパズルの組み合わせ
|
ユーザー |
![]() |
提出日時 | 2020-03-04 17:27:53 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 791 ms / 2,000 ms |
コード長 | 1,110 bytes |
コンパイル時間 | 278 ms |
コンパイル使用メモリ | 12,672 KB |
実行使用メモリ | 124,388 KB |
最終ジャッジ日時 | 2024-10-14 00:22:59 |
合計ジャッジ時間 | 71,931 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 99 |
ソースコード
#!/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 # %% M, *H = map(int, read().split()) # %% 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 # %% x = M - sum(H) - len(H) + 1 if x < 0: print('NA') exit() if H[0] == 0: print(1) exit() n = len(H) + 1 # %% fact, fact_inv = make_fact(2 * 10 ** 6) answer = fact[x + n - 1] * fact_inv[x] % MOD * fact_inv[n - 1] % MOD print(answer)