結果

問題 No.840 ほむほむほむら
ユーザー face4face4
提出日時 2019-11-24 10:17:39
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 831 ms / 4,000 ms
コード長 1,309 bytes
コンパイル時間 693 ms
コンパイル使用メモリ 75,548 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-10-12 04:16:06
合計ジャッジ時間 5,790 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

// 自力AC
#include<iostream>
#include<vector>
using namespace std;

typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
ll mod = 998244353;

mat mul(const mat &A, const mat &B){
    mat C(A.size(), vec(B[0].size()));
    for(int i = 0; i < A.size(); i++){
        for(int k = 0; k < B.size(); k++){
            for(int j = 0; j < B[0].size(); j++){
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod;
            }
        }
    }
    return C;
}

mat pow(mat A, ll n){
    mat B(A.size(), vec(A.size()));
    for(int i = 0; i < A.size(); i++){
        B[i][i] = 1ll;
    }
    while(n > 0){
        if(n & 1)   B = mul(B, A);
        A = mul(A, A);
        n >>= 1;
    }
    return B;
}

int main(){
    int n, k;
    cin >> n >> k;
    int siz = k*k*k;
    mat A(siz, vec(siz, 0)), B(siz, vec(1,0));
    B[0][0] = 1;
    for(int i = 0; i < siz; i++){
        int x[3], cp = i;
        for(int j = 2; j >= 0; j--){
            x[j] = cp%k;
            cp /= k;
        }
        A[x[0]*k*k+x[1]*k+(x[2]+1)%k][i]++;
        A[x[0]*k*k+(x[1]+x[2])%k*k+x[2]][i]++;
        A[(x[0]+x[1])%k*k*k+x[1]*k+x[2]][i]++;
    }
    mat C = mul(pow(A, n), B);
    ll ans = 0;
    for(int i = 0; i < k*k; i++){
        (ans += C[i][0]) %= mod;
    }
    cout << ans << endl;
    return 0;
}
0