結果

問題 No.2717 Sum of Subarray of Subsequence
ユーザー InTheBloomInTheBloom
提出日時 2024-04-06 00:29:17
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 44 ms / 2,000 ms
コード長 2,604 bytes
コンパイル時間 5,504 ms
コンパイル使用メモリ 195,924 KB
実行使用メモリ 13,756 KB
最終ジャッジ日時 2024-04-06 00:29:24
合計ジャッジ時間 7,190 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 1 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 1 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 41 ms
11,708 KB
testcase_10 AC 40 ms
11,708 KB
testcase_11 AC 41 ms
11,708 KB
testcase_12 AC 41 ms
11,708 KB
testcase_13 AC 41 ms
11,708 KB
testcase_14 AC 44 ms
11,708 KB
testcase_15 AC 40 ms
11,708 KB
testcase_16 AC 41 ms
11,708 KB
testcase_17 AC 40 ms
11,708 KB
testcase_18 AC 41 ms
11,708 KB
testcase_19 AC 41 ms
11,708 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 1 ms
6,676 KB
testcase_22 AC 41 ms
13,756 KB
testcase_23 AC 20 ms
6,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std;

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

    // 解説AC
    // 各項寄与を考える。以降1-indexed
    // A[i]を採用し、A[i]の左からl項、右からr項抜いてきた部分列はcomb(i - 1, l) * comb(N - i, r)通り
    // そのような部分列の中からA[i]を含む連続部分列は(l + 1) * (r + 1)通りとれる。(左、右から0 ~ 項採用する感じ)
    // したがって、(0 <= l <= i - 1), (0 <= r <= N - i) comb(i - 1, l) * comb(N - i, r) * (l + 1) * (r + 1)がある程度の速度で計算できればOK
    // lとrを分離すると、次のようになる。 { (0 <= l <= i - 1) comb(i - 1, l) * (l + 1) } * { (0 <= r <= N - i) comb(N - i, r) * (r + 1) }
    // これをうまく変形する。
    // wolfram alphaに投げるなり調べるなりすると、
    // { (0 <= l <= i - 1) (i - 1) * comb(i - 2, l - 1) + comb(i - 1, l) } * { (0 <= r <= N - i) (N - i) * comb(N - i - 1, r - 1) + comb(N - i, r) }
    // = { (i - 1) * 2^(i - 2) + 2^(i - 1) } * { (N - i) * 2^(N - i - 1) + 2^(N - i) }
    // = { (i - 1) * 2^(i - 2) + 2 * 2^(i - 2) } * { (N - i) * 2^(N - i - 1) + 2 * 2^(N - i - 1) }
    // = (i + 1) * 2^(i - 2) * (N - i + 2) * 2^(N - i - 1)
    // = (i + 1) * (N - i + 2) * 2^(N - 3)
    // クエリO(1)になり、これで解ける。

    solve(N, A);
}

void solve (int N, int[] A) {
    const long MOD = 998244353;
    long prod;
    if (0 <= N - 3) {
        prod = mod_pow(2, N - 3, MOD);
    }
    else {
        prod = mod_pow(mod_inv(2, MOD), abs(N - 3), MOD);
    }

    long ans = 0;
    foreach (i; 0..N) {
        ans += 1L * A[i] * (i + 2) % MOD * (N - i + 1) % MOD * prod % MOD;

        ans %= MOD;
    }

    writeln(ans);
}


long mod_pow (long a, long x, const long MOD)
in {
    assert(0 <= x, "x must satisfy 0 <= x");
    assert(1 <= MOD, "MOD must satisfy 1 <= MOD");
    assert(MOD <= int.max, "MOD must satisfy MOD*MOD <= long.max");
}
do {
    // normalize
    a %= MOD; a += MOD; a %= MOD;

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

    return res % MOD;
}

// check mod_pow
static assert(__traits(compiles, mod_pow(2, 10, 998244353)));

long mod_inv (const long x, const long MOD)
in {
    import std.format : format;
    assert(1 <= MOD, format("MOD must satisfy 1 <= MOD. Now MOD =  %s.", MOD));
    assert(MOD <= int.max, format("MOD must satisfy MOD*MOD <= long.max. Now MOD = %s.", MOD));
}
do {
    return mod_pow(x, MOD-2, MOD);
}
0