結果

問題 No.840 ほむほむほむら
ユーザー nebukuro09nebukuro09
提出日時 2019-06-14 23:10:22
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 595 ms / 4,000 ms
コード長 1,581 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 101,284 KB
実行使用メモリ 7,792 KB
最終ジャッジ日時 2023-09-04 01:43:28
合計ジャッジ時間 5,110 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 13 ms
4,380 KB
testcase_03 AC 82 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 26 ms
4,376 KB
testcase_08 AC 148 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 41 ms
4,380 KB
testcase_13 AC 385 ms
7,248 KB
testcase_14 AC 49 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 7 ms
4,380 KB
testcase_17 AC 90 ms
4,376 KB
testcase_18 AC 488 ms
7,172 KB
testcase_19 AC 595 ms
7,740 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 9 ms
4,380 KB
testcase_23 AC 569 ms
7,792 KB
testcase_24 AC 9 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 13 ms
4,376 KB
testcase_27 AC 569 ms
7,772 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.string;

immutable long MOD = 998244353 ;

void main() {
    auto s = readln.split.map!(to!int);
    auto N = s[0];
    auto K = s[1];
    auto M = K ^^ 3;

    auto mat = new long[][](M, M);
    foreach (mask; 0..M) {
        int c = mask % K;
        int b = mask / K % K;
        int a = mask / K / K;
        int na = (a + 1) % K;
        int nb = (b + a) % K;
        int nc = (c + b) % K;
        int x = na * K^^2 + b * K + c;
        int y = a * K^^2 + nb * K + c;
        int z = a * K^^2 + b * K + nc;
        mat[x][mask] += 1;
        mat[y][mask] += 1;
        mat[z][mask] += 1;
    }

    long ans = 0;
    auto nmat = matpow(M, N, mat);
    foreach (mask; 0..M) if (mask % K == 0) ans = (ans + nmat[mask][0]) % MOD;
    ans.writeln;
}

long[][] matmul(int N, long[][] m1, long[][] m2) {
    auto ret = new long[][](N, N);
    foreach (i; 0..N) {
        foreach (j; 0..N) {
            ret[i][j] = 0;
            foreach (k; 0..N) {
                (ret[i][j] += m1[i][k] * m2[k][j] % MOD) %= MOD;
            }
        }
    }
    return ret;
}

long[][] matpow(int N, long K, long[][] mat) {
    auto ret = new long[][](N, N);
    foreach (i; 0..N)
        foreach (j; 0..N)
            ret[i][j] = i == j ? 1 : 0;

    while (K > 0) {
        if (K % 2 == 1)
            ret = matmul(N, ret, mat);
        mat = matmul(N, mat, mat);
        K /= 2;
    }

    return ret;
}
0