結果

問題 No.3202 Periodic Alternating Subsequence
ユーザー YY-otter
提出日時 2025-07-03 10:30:38
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,718 bytes
コンパイル時間 937 ms
コンパイル使用メモリ 108,032 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-07-03 21:35:02
合計ジャッジ時間 1,858 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other WA * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;
using ll = long long;
// 状態ベクトルの次元 (C0,L0,Q0, C1,L1,Q1, const 1)
const int DIM = 7;
const int MOD = 1e9 + 7;

// 7x7行列の型エイリアス
using Matrix = vector<vector<ll>>;

// 行列積 C = A * B
Matrix multiply(const Matrix& A, const Matrix& B) {
    Matrix C(DIM, vector<ll>(DIM, 0));
    for (int i = 0; i < DIM; ++i) {
        for (int j = 0; j < DIM; ++j) {
            for (int k = 0; k < DIM; ++k) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
            }
        }
    }
    return C;
}

// 行列の累乗 A^k
Matrix power(Matrix A, ll k) {
    Matrix res(DIM, vector<ll>(DIM, 0));
    for (int i = 0; i < DIM; ++i) res[i][i] = 1; // 単位行列で初期化
    
    while (k > 0) {
        if (k & 1) res = multiply(res, A);
        A = multiply(A, A);
        k >>= 1;
    }
    return res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    ll K;
    cin >> K;
    string T;
    cin >> T;

    // --- 遷移行列の定義 ---

    // 文字 '0' を処理する遷移行列 M0
    Matrix M0(DIM, vector<ll>(DIM, 0));
    M0[0][0] = 1; M0[0][3] = 1; M0[0][6] = 1;
    M0[1][1] = 1; M0[1][3] = 1; M0[1][4] = 1; M0[1][6] = 1;
    M0[2][2] = 1; M0[2][3] = 1; M0[2][4] = 2; M0[2][5] = 1; M0[2][6] = 1;
    M0[3][3] = 1;
    M0[4][4] = 1;
    M0[5][5] = 1;
    M0[6][6] = 1;

    // 文字 '1' を処理する遷移行列 M1
    Matrix M1(DIM, vector<ll>(DIM, 0));
    M1[0][0] = 1;
    M1[1][1] = 1;
    M1[2][2] = 1;
    M1[3][0] = 1; M1[3][3] = 1; M1[3][6] = 1;
    M1[4][0] = 1; M1[4][1] = 1; M1[4][4] = 1; M1[4][6] = 1;
    M1[5][0] = 1; M1[5][1] = 2; M1[5][2] = 1; M1[5][5] = 1; M1[5][6] = 1;
    M1[6][6] = 1;
    
    // --- 計算 ---

    // 基本文字列 T を1回処理する遷移行列 MT を計算
    Matrix MT(DIM, vector<ll>(DIM, 0));
    for(int i = 0; i < DIM; ++i) MT[i][i] = 1; // 単位行列で初期化

    // V_new = M_char * V_old のため、行列の積は M_T = M_{last} * ... * M_{first}
    // 文字列を逆順に処理して行列を掛ける
    string T_rev = T;
    reverse(T_rev.begin(), T_rev.end());

    for (char c : T_rev) {
        if (c == '0') {
            MT = multiply(M0, MT);
        } else {
            MT = multiply(M1, MT);
        }
    }

    // MTをK乗する
    Matrix M_final = power(MT, K);

    // 初期ベクトル [0,0,0,0,0,0,1]^T との積の結果は、M_final の最終列に現れる
    ll final_Q0 = M_final[2][6];
    ll final_Q1 = M_final[5][6];

    // 最終的なスコアの合計を出力
    cout << (final_Q0 + final_Q1) % MOD << endl;

    return 0;
}
0