結果
問題 | No.503 配列コレクション |
ユーザー |
![]() |
提出日時 | 2020-04-08 12:41:14 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 675 ms / 2,000 ms |
コード長 | 1,505 bytes |
コンパイル時間 | 88 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 122,620 KB |
最終ジャッジ日時 | 2024-07-18 09:38:24 |
合計ジャッジ時間 | 16,980 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 25 |
ソースコード
#!/usr/bin/ python3.8 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 N, K, D = 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 def make_power(a, L, MOD=MOD): B = L.bit_length() x = np.empty((1 << B), np.int64) x[0] = 1 for n in range(B): x[1 << n:1 << (n + 1)] = x[:1 << n] * a % MOD a *= a a %= MOD x = x[:L] x.flags.writeable = False return x L = N % (K - 1) if not L: L = K - 1 S = (N - L) // (K - 1) if L == 1: answer = pow(D, S, MOD) print(answer) exit() fact, fact_inv = make_fact(2 * 10 ** 6 + 10) power = make_power(D, S + 1) coef = fact[L - 2: L + S - 1][::-1] * fact_inv[0: S + 1][::-1] % MOD * fact_inv[L - 2] % MOD x = (coef * power % MOD).sum() % MOD answer = x * L % MOD if D == 1: answer = L print(answer)