結果
| 問題 |
No.2959 Dolls' Tea Party
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 14:04:13 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,159 bytes |
| コンパイル時間 | 391 ms |
| コンパイル使用メモリ | 82,408 KB |
| 実行使用メモリ | 130,168 KB |
| 最終ジャッジ日時 | 2025-06-12 14:04:46 |
| 合計ジャッジ時間 | 6,341 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 2 TLE * 1 -- * 30 |
ソースコード
import sys
MOD = 998244353
def main():
N, K = map(int, sys.stdin.readline().split())
A = list(map(int, sys.stdin.readline().split()))
# Precompute divisors of K
divisors = []
for i in range(1, int(K**0.5)+1):
if K % i == 0:
divisors.append(i)
if i != K // i:
divisors.append(K // i)
divisors.sort()
# Precompute inv_fact up to 1300
max_m = 1300
inv_fact = [1] * (max_m + 1)
# Precompute factorials modulo MOD
fact = [1] * (max_m + 1)
for i in range(1, max_m + 1):
fact[i] = fact[i-1] * i % MOD
# Compute inverse factorials using Fermat's little theorem
inv_fact[max_m] = pow(fact[max_m], MOD-2, MOD)
for i in range(max_m-1, -1, -1):
inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
# Precompute Euler's totient function for all divisors of K
def compute_phi(n):
result = n
i = 2
while i*i <= n:
if n % i == 0:
while n % i == 0:
n //= i
result -= result // i
i += 1
if n > 1:
result -= result // n
return result
phi = {d: compute_phi(d) for d in divisors}
total = 0
for d in divisors:
m = K // d
if m == 0:
continue
# Precompute B_i = A_i // d for each doll type
B = [a // d for a in A]
current_dp = [0] * (m + 1)
current_dp[0] = 1
for b in B:
next_dp = [0] * (m + 1)
for j in range(m + 1):
if current_dp[j] == 0:
continue
max_c = min(b, m - j)
for c in range(0, max_c + 1):
if j + c > m:
continue
next_dp[j + c] = (next_dp[j + c] + current_dp[j] * inv_fact[c]) % MOD
current_dp = next_dp
S = current_dp[m]
F_d = S * fact[m] % MOD
total = (total + phi[d] * F_d) % MOD
ans = total * pow(K, MOD-2, MOD) % MOD
print(ans)
if __name__ == '__main__':
main()
gew1fw