結果

問題 No.840 ほむほむほむら
ユーザー tsutajtsutaj
提出日時 2019-06-14 21:57:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 146 ms / 4,000 ms
コード長 4,942 bytes
コンパイル時間 1,227 ms
コンパイル使用メモリ 115,824 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-08 23:39:18
合計ジャッジ時間 3,238 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 5 ms
4,376 KB
testcase_03 AC 21 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 7 ms
4,380 KB
testcase_08 AC 37 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 12 ms
4,384 KB
testcase_13 AC 95 ms
4,376 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 23 ms
4,380 KB
testcase_18 AC 121 ms
4,380 KB
testcase_19 AC 146 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 4 ms
4,376 KB
testcase_23 AC 141 ms
4,380 KB
testcase_24 AC 3 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 5 ms
4,380 KB
testcase_27 AC 139 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 998244353LL;

int nxt[5][5][5][5][5][5];
int to_int(int a, int b, int c, int K) {
    int res = a;
    res = res * K + b;
    res = res * K + c;
    return res;
}

// 行列ライブラリ
// Verified: TopCoder SRM 704 Div.2 (ModEquationEasy)
// 行列の積 と 累乗(繰り返し二乗法)

// Matrix Library Begin (C++11)

template <typename T>
using Matrix = vector< vector<T> >;

template <typename T>
void init_mat(Matrix<T> &A, int h, int w) {
    A.resize(h, vector<T>(w, 0));
}

template <typename T>
Matrix<T> calc_mat(Matrix<T> A, Matrix<T> B) {
    Matrix<T> C(A.size(), vector<T>(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] += A[i][k] * B[k][j]; // modなし
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD; // modあり
            }
        }
    }
    return C;
}

template <typename T>
Matrix<T> mat_pow(Matrix<T> A, ll n) {
    Matrix<T> B(A.size(), vector<T>(A.size()));
    for(int i=0; i<A.size(); i++) B[i][i] = 1;
    while(n > 0) {
        if(n & 1) B = calc_mat(B, A);
        A = calc_mat(A, A);
        n >>= 1;
    }
    return B;
}

// N * M 行列のランクを求める
// 整数行列でも基本変形時に分数になるので double 型に限定しています
// 位数 2 の有限体では (演算を xor とかに直した形で) 確認済み、他は未 Verify なのでもしかしたらやばいかもしれない
int mat_rank(Matrix<double> A) {
    int N = A.size(), M = A[0].size();

    int res = 0;
    for(int i=0; i<M; i++) {
        // 絶対値が最も大きいものを探す
        double max_elem = 0; int piv = -1;
        for(int j=res; j<N; j++) {
            if(abs(A[j][i]) > max_elem) {
                max_elem = abs(A[j][i]);
                piv = j;
            }
        }

        // res 行目を piv 行目に変えて、行基本変形
        if(piv < 0) continue;
        swap(A[res], A[piv]);

        // res 行目を正規化
        // F_2 の場合はここは不要
        {
            double div = A[res][i];
            for(int k=0; k<M; k++) {
                A[res][k] = 1.0 * A[res][k] / div;
            }
        }
        
        for(int j=0; j<N; j++) {
            if(j == res) continue;
            assert(A[res][i] != 0);
            double mul = A[j][i];
            for(int k=0; k<M; k++) {
                // 場合に応じて xor に変えるなりする
                A[j][k] = A[j][k] - mul * A[res][k];
            }
        }
        res++;
    }
    return res;
}

// Matrix Library End
 
int main() {
    int64 N, K; cin >> N >> K;
    int64 dim = K * K * K;
    
    for(int a=0; a<K; a++) {
        for(int b=0; b<K; b++) {
            for(int c=0; c<K; c++) {
                // A にする
                {
                    nxt[a][b][c][(a+1)%K][b][c]++;
                }

                // B にする
                {
                    nxt[a][b][c][a][(a+b)%K][c]++;
                }

                // C にする
                {
                    nxt[a][b][c][a][b][(b+c)%K]++;
                }
            }
        }
    }

    Matrix<int64> mat;
    init_mat(mat, dim, dim);
    for(int a=0; a<K; a++) {
        for(int b=0; b<K; b++) {
            for(int c=0; c<K; c++) {
                for(int na=0; na<K; na++) {
                    for(int nb=0; nb<K; nb++) {
                        for(int nc=0; nc<K; nc++) {
                            int ix = to_int( a,  b,  c, K);
                            int iy = to_int(na, nb, nc, K);
                            mat[ix][iy] = nxt[a][b][c][na][nb][nc];
                        }
                    }
                }
            }
        }
    }

    Matrix<int64> vec;
    init_mat(vec, dim, 1);
    vec[0][0] = 1;

    int64 ans = 0;
    vec = calc_mat(mat_pow(mat, N), vec);
    for(int i=0; i<dim; i++) {
        int M = i;
        if(M % K == 0) {
            (ans += vec[i][0]) %= MOD;
        }
    }

    cout << ans << endl;
    return 0;
}
0