結果

問題 No.2550 MORE! JUMP! MORE!
ユーザー Theta
提出日時 2024-03-06 18:15:32
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,457 ms / 2,000 ms
コード長 1,769 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 97,256 KB
最終ジャッジ日時 2024-09-29 18:31:50
合計ジャッジ時間 24,791 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import cache
from re import M
import sys


def printe(*args, end="\n", **kwargs):
    print(*args, end=end, file=sys.stderr, **kwargs)


@cache
def pow_mod(base: int, idx: int, mod: int) -> int:
    if idx == 0:
        return 1 % mod

    if idx == 1:
        return base % mod

    if idx % 2 == 0:
        idx //= 2
        base **= 2
        base %= mod
        return pow_mod(base, idx, mod)

    return base * pow_mod(base, idx - 1, mod)


def main():
    N = int(input())
    A = list(map(int, input().split()))
    MOD = 998244353
    if N == 1:
        print(A[0] % MOD)
        return
    # 書き出してみると各ステップごとに到達するときの上り方のパターン数は、kステップについて考えると二項係数になっている
    # →ステップ数 ↓行動回数
    # 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    # 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
    # 0 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 0
    # 0 0 0 1 3 6 10 15 21 28 36 45 55 66 78 91 0
    # 0 0 0 0 1 4 10 20 35 56 84 120 165 220 286 364 0
    # ...

    # kステップ目におけるi*A_kの和を求めるには、kC0+2*kC1+3*kC2+...+(k+1)kCk
    # がA_kの係数になるが、これは二項係数に関する和の公式から
    # https://a-iine.net/combination/
    # (k+2)*2^(k-1)と整理できる

    patterns = A[0] * pow_mod(2, N - 2, MOD)
    for idx, a_elm in enumerate(A[1:], 1):
        printe(patterns)
        if idx < N - 1:
            patterns += (idx + 2) * pow_mod(2, idx - 1, MOD) * \
                a_elm * pow_mod(2, N - idx - 2, MOD)
        else:
            patterns += (idx + 2) * pow_mod(2, idx - 1, MOD) * \
                a_elm
        patterns %= MOD

    print(patterns)


if __name__ == "__main__":
    main()
0