結果

問題 No.1540 級数おもちゃ
ユーザー lam6er
提出日時 2025-03-20 20:43:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 277 ms / 2,000 ms
コード長 1,536 bytes
コンパイル時間 325 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 92,380 KB
最終ジャッジ日時 2025-03-20 20:43:42
合計ジャッジ時間 10,762 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
max_j = 10**5

# Precompute pow2: pow2[j] = 2^j mod mod
pow2 = [1] * (max_j + 1)
for j in range(1, max_j + 1):
    pow2[j] = (pow2[j-1] * 2) % mod

max_n = max_j  # n ranges from 0 to max_j - 1

a = [0] * max_n
b = [0] * max_n
c = [0] * max_n

# n = 0
a[0] = pow(6, mod-2, mod)  # 1/6 mod mod
b[0] = 0
c[0] = 0

# n = 1
if max_n >= 1:
    a[1] = 0
    inv2 = pow(2, mod-2, mod)
    b[1] = (mod - inv2) % mod
    c[1] = 1

# Precompute a, b, c for n >= 2 up to max_n-1
for n in range(2, max_n):
    inv_n = pow(n, mod-2, mod)
    term1 = ((n - 1) * inv_n) % mod
    
    # a[n] = term1 * a[n-2]
    a[n] = (term1 * a[n-2]) % mod
    
    # b[n] = term1 * b[n-2] - 1/(2^n * n)
    term_b1 = (term1 * b[n-2]) % mod
    denominator = (pow2[n] * n) % mod
    inv_denominator = pow(denominator, mod-2, mod)
    term_b2 = (mod - inv_denominator) % mod  # -1/(2^n *n)
    b[n] = (term_b1 + term_b2) % mod
    
    # c[n] = term1 * c[n-2]
    c[n] = (term1 * c[n-2]) % mod

# Read input
import sys
input = sys.stdin.read().split()
N = int(input[0])
A = list(map(int, input[1:N+1]))

sum_a = 0
sum_b = 0
sum_c = 0

for idx in range(N):
    j = idx + 1
    Aj = A[idx]
    n = j - 1
    if n >= max_n:
        pass
    else:
        current_a = (c[n] * pow2[j]) % mod
        current_b = (b[n] * pow2[j]) % mod
        current_c = (a[n] * pow2[j]) % mod
        
        sum_a = (sum_a + Aj * current_a) % mod
        sum_b = (sum_b + Aj * current_b) % mod
        sum_c = (sum_c + Aj * current_c) % mod

print(sum_a, sum_b, sum_c)
0