結果

問題 No.3202 Periodic Alternating Subsequence
ユーザー t98slider
提出日時 2025-07-11 22:47:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 160 ms / 2,000 ms
コード長 3,336 bytes
コンパイル時間 4,284 ms
コンパイル使用メモリ 255,592 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-07-11 22:47:42
合計ジャッジ時間 8,463 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using ll = long long;
using mint = atcoder::modint1000000007;

template <class T, size_t N> struct Matrix {
    std::array<std::array<T, N>, N> A{};
    Matrix() {}
    Matrix(const std::array<std::array<T, N>, N> &M) : A(M){}
    Matrix(const std::vector<std::vector<T>> &M)  {
        for(size_t i = 0; i < N; i++){
            for(size_t j = 0; j < N; j++){
                A[i][j] = M[i][j];
            }
        }
    }
    const std::array<T, N>& operator[](int i) const { return A[i]; }
    std::array<T, N>& operator[](int i) { return A[i]; }

    Matrix& operator+=(const Matrix& rhs) {
        for(size_t i = 0; i < N; i++){
            for(size_t j = 0; j < N; j++){
                A[i][j] += rhs[i][j];
            }
        }
        return *this;
    }
    Matrix& operator-=(const Matrix& rhs) {
        for(size_t i = 0; i < N; i++){
            for(size_t j = 0; j < N; j++){
                A[i][j] -= rhs[i][j];
            }
        }
        return *this;
    }
    Matrix& operator*=(const Matrix& rhs) {
        std::array<std::array<T, N>, N> res{};
        for(size_t i = 0; i < N; i++){
            for(size_t j = 0; j < N; j++){
                for(size_t k = 0; k < N; k++){
                    res[i][j] += A[i][k] * rhs[k][j];
                }
            }
        }
        swap(A, res);
        return *this;
    }
    Matrix& operator+() const { return *this; }
    Matrix& operator-() const { return Matrix() - *this; }
    friend Matrix operator+(const Matrix& lhs, const Matrix& rhs) {
        return Matrix(lhs) += rhs;
    }
    friend Matrix operator-(const Matrix& lhs, const Matrix& rhs) {
        return Matrix(lhs) -= rhs;
    }
    friend Matrix operator*(const Matrix& lhs, const Matrix& rhs) {
        return Matrix(lhs) *= rhs;
    }
    friend bool operator==(const Matrix& lhs, const Matrix& rhs) {
        return (lhs.A == rhs.A);
    }
    friend bool operator!=(const Matrix& lhs, const Matrix& rhs) {
        return (lhs.A != rhs.A);
    }
    Matrix pow(long long v){
        Matrix res, temp = A;
        for(size_t i = 0; i < N; i++)res[i][i] = 1;
        while(v){
            if(v & 1)res *= temp;
            temp *= temp;
            v >>= 1;
        }
        return res;
    }
    friend std::ostream& operator << (std::ostream &os, const Matrix& rhs) noexcept {
        for(size_t i = 0; i < N; i++){
            if(i) os << '\n';
            for(size_t j = 0; j < N; j++){
                os << (j ? " " : "") << rhs[i][j];
            }
        }
        return os;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    ll k;
    cin >> s >> k;

    Matrix<mint, 7> M0, M1, M;
    for(int i = 0; i < 7; i++){
        M0[i][i] = 1;
        M1[i][i] = 1;
        M[i][i] = 1;
    }
    // (0, 1, 2), (3, 4, 5)
    M0[2][6] = 1;
    M0[2][5] = 1;
    M0[1][5] = 2;
    M0[1][4] = 1;
    M0[0][3] = 1;
    M0[0][4] = 1;
    M0[0][5] = 1;

    M1[5][6] = 1;
    M1[5][2] = 1;    
    M1[4][1] = 1;
    M1[4][2] = 2;
    M1[3][0] = 1;
    M1[3][1] = 1;
    M1[3][2] = 1;

    for(auto c: s){
        M *= c == '0' ? M0 : M1;
    }
    auto Mt = M.pow(k);
    mint ans = 0;
    for(int i = 0; i < 6; i++){
        ans += Mt[i][6];
    }
    cout << ans.val() << '\n';
}
0