結果

問題 No.3571 Sigma Problems Problem
ユーザー InTheBloom
提出日時 2026-06-23 06:35:51
言語 D
(dmd 2.112.0)
コンパイル:
dmd -fPIE -m64 -w -wi -O -release -inline -I/opt/dmd/src/druntime/import/ -I/opt/dmd/src/phobos -L-L/opt/dmd/linux/lib64/ -fPIC _filename_
実行:
./Main
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 2,242 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,339 ms
コンパイル使用メモリ 194,216 KB
実行使用メモリ 7,968 KB
最終ジャッジ日時 2026-06-23 06:36:00
合計ジャッジ時間 7,940 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import std;

void main () {
    const long MOD = 998244353;
    int caseNum = readln.chomp.to!int;

    const long inv2 = mod_inv(2, MOD);

    auto ans = new long[](caseNum);
    foreach (caseId; 0 .. caseNum) {
        long N, M;
        readln.read(N, M);

        if (N == 1) {
            ans[caseId] = 0;
            continue;
        }

        // あるペア(i, j)に着目
        // 対称性より、あるペアの解はM^(N - 2)倍になる。
        // Ai + Aj = x (mod M)となるような数は
        // Ai + Aj = xとなるのがx + 1通り
        // Ai + Aj = M + xとなるのがM + x - (M - 1) = x + 1よりM - x - 1通り

        // よって、ペア(i, j)の寄与は、
        // 0 <= x <= M - 1に対してx * (x + 1 + (M - x - 1)) = M * x
        // 総和はM * M * (M - 1) / 2通りになる

        long mm = M % MOD;
        long v = mm * mm % MOD * (mm - 1) % MOD * inv2 % MOD;
        v = v * mod_pow(M, N - 2, MOD) % MOD;

        long nn = N % MOD;
        long p = nn * (nn - 1) % MOD * inv2 % MOD;

        long ret = v * p % MOD;
        if (ret < 0) {
            ret += MOD;
        }

        ans[caseId] = ret;
    }

    writefln("%(%s\n%)", ans);
}

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

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