結果

問題 No.2550 MORE! JUMP! MORE!
ユーザー InTheBloom
提出日時 2023-11-20 19:15:49
言語 D
(dmd 2.109.1)
結果
AC  
実行時間 274 ms / 2,000 ms
コード長 1,458 bytes
コンパイル時間 6,082 ms
コンパイル使用メモリ 206,464 KB
実行使用メモリ 10,812 KB
最終ジャッジ日時 2024-09-26 06:41:31
合計ジャッジ時間 8,287 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

void main () {
    int N = readln.chomp.to!int;
    int[] A = readln.split.to!(int[]);

    solve(N, A);
}

void solve (int N, int[] A) {
    const int MOD = 998244353;

    long ans = 0;
    foreach (i, a; A) {
        long x = cast(int) i + 1;
        long term1 = modPow(2, x-1, MOD);
        long term2 = 0;
        if (2 <= x) term2 = (x-1) * modPow(2, x-2, MOD) % MOD;
        long sum = (term1+term2) % MOD;
        if (x < N) {
            sum *= modPow(2, N-x-1, MOD);
            sum %= MOD;
            ans += a*sum % MOD;
        }
        else {
            ans += a*sum % MOD;
        }

        // dbg
        //writeln("x: ", x, " val: ", a*(term1+term2));
        ans %= MOD;
    }

    writeln(ans);
}

void read(T...)(string S, ref T args) {
    auto buf = S.split;
    foreach (i, ref arg; args) {
        arg = buf[i].to!(typeof(arg));
    }
}

long modPow (long a, long x, const int MOD) {
    // assertion
    assert(0 <= x, format("x = %d", x));
    assert(1 <= MOD);

    // normalize
    a %= MOD; a += MOD; a %= MOD;

    // simple case
    if (MOD == 1) {
        return 0L;
    }

    if (x == 0) {
        return 1L;
    }

    if (x == 1) {
        return a;
    }

    // calculate
    long res = 1L;
    long base = a % MOD;
    while (x != 0) {
        if ((x&1) != 0) {
            res *= base;
            res %= MOD;
        }
        base = base*base; base %= MOD;
        x >>= 1;
    }

    return res;
}
0