結果

問題 No.840 ほむほむほむら
ユーザー t33ft33f
提出日時 2019-06-26 23:34:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 149 ms / 4,000 ms
コード長 1,619 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 66,584 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-08 13:41:28
合計ジャッジ時間 3,821 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 5 ms
4,380 KB
testcase_03 AC 22 ms
4,380 KB
testcase_04 AC 2 ms
4,384 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 38 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 97 ms
4,376 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 3 ms
4,384 KB
testcase_17 AC 23 ms
4,380 KB
testcase_18 AC 122 ms
4,380 KB
testcase_19 AC 149 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 143 ms
4,376 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 4 ms
4,376 KB
testcase_27 AC 142 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstring>
#include <iostream>
using namespace std;
int n, k, s;
constexpr int M = 998244353;

inline int index(int x, int y, int z) { return x + k * (y + k * z); }

void mmul(long long *X, const long long *A, const long long *B) {
    static long long *tmp = nullptr;
    if (tmp == nullptr) tmp = new long long[s*s];
    for (int i = 0; i < s; i++)
        for (int j = 0; j < s; j++) {
            long long sum = 0;
            for (int k = 0; k < s; k++) sum += A[i*s+k] * B[k*s+j] % M;
            tmp[i*s+j] = sum % M;
        }
    memcpy(X, tmp, sizeof(*X)*s*s);
}

void mpow(long long *A, int k) {
    static long long *tmp = nullptr, *P = nullptr;
    if (tmp == nullptr) tmp = new long long[s*s];
    if (P == nullptr) P = new long long[s*s];
    for (int i = 0; i < s; i++)
        for (int j = 0; j < s; j++)
            tmp[s*i + j] = (i==j), P[s*i+j] = A[s*i+j];
    while (k > 0) {
        if (k % 2) mmul(tmp, tmp, P);
        mmul(P, P, P);
        k /= 2;
    }
    memcpy(A, tmp, sizeof(*A)*s*s);
}

int main() {
    cin >> n >> k;
    s = k*k*k;
    long long A[s][s];
    memset(A, 0, sizeof A);
    for (int x = 0; x < k; x++)
        for (int y = 0; y < k; y++)
            for (int z = 0; z < k; z++) {
                int i = index(x, y, z);
                A[index((x+1)%k, y, z)][i]++;
                A[index(x, (y+x)%k, z)][i]++;
                A[index(x, y, (z+y)%k)][i]++;
            }

    mpow(&A[0][0], n);
    long long ans = 0;
    for (int x = 0; x < k; x++)
        for (int y = 0; y < k; y++)
            ans += A[index(x, y, 0)][0] % M;

    cout << ans%M << endl;
}
0