結果

問題 No.840 ほむほむほむら
コンテスト
ユーザー 梧桐
提出日時 2026-02-23 22:34:46
言語 C++14
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++14 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 155 ms / 4,000 ms
コード長 1,514 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 607 ms
コンパイル使用メモリ 77,848 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-23 22:34:48
合計ジャッジ時間 2,391 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <cstdio>

using namespace std;

typedef long long LL;

const int N = 130;
const LL MOD = 998244353;

LL n, ans;
int k, sz;

struct Matrix {
    int v[N][N];
    Matrix operator *(Matrix o) {
        Matrix r = {};
        for (int i = 1; i <= sz; ++i) {
            for (int j = 1; j <= sz; ++j) {
                for (int k = 1; k <= sz; ++k) {
                    r.v[i][k] = (r.v[i][k] + 1LL * v[i][j] * o.v[j][k]) % MOD;
                }
            }
        }
        return r;
    }
    Matrix operator *=(Matrix o) {
        return *this = *this * o;
    }
};
Matrix mt;

Matrix QMI(Matrix x, LL m) {
    Matrix ret = {};
    for (int i = 1; i <= sz; ++i) ret.v[i][i] = 1;
    while (m) {
        if (m & 1) ret *= x;
        x *= x;
        m >>= 1;
    }
    return ret;
}

int main() {
    scanf("%lld%d", &n, &k);

    sz = k * k * k;
    for (int a = 0; a < k; ++a) {
        for (int b = 0; b < k; ++b) {
            for (int c = 0; c < k; ++c) {
                int from = a * k * k + b * k + c + 1;
                ++mt.v[((a + 1) % k) * k * k + b * k + c + 1][from];
                ++mt.v[a * k * k + ((b + a) % k) * k + c + 1][from];
                ++mt.v[a * k * k + b * k + (c + b) % k + 1][from];
            }
        }
    }

    mt = QMI(mt, n);

    ans = 0LL;
    for (int a = 0; a < k; ++a) {
        for (int b = 0; b < k; ++b) {
            ans = (ans + mt.v[a * k * k + b * k + 1][1]) % MOD;
        }
    }
    printf("%lld\n", ans);

    return 0;
}
0