結果

問題 No.3202 Periodic Alternating Subsequence
ユーザー otoshigo
提出日時 2025-07-12 10:49:23
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 254 ms / 2,000 ms
コード長 3,777 bytes
コンパイル時間 3,458 ms
コンパイル使用メモリ 282,844 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2025-07-12 10:49:37
合計ジャッジ時間 8,944 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (ll i = s; i < (ll)(t); i++)
#define rrep(i, s, t) for(ll i = (ll)(t) - 1; i >= (ll)(s); i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)

#define TT template<typename T>
TT using vec = vector<T>;
template<class T1, class T2> bool chmin(T1 &x, T2 y) { return x > y ? (x = y, true) : false; }
template<class T1, class T2> bool chmax(T1 &x, T2 y) { return x < y ? (x = y, true) : false; }

struct io_setup {
    io_setup() {
        ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
        cout << fixed << setprecision(15);
    }
} io_setup;

template <class T>
struct Matrix {
    vector<vector<T>> A;
    Matrix(int n, int m) : A(n, vector<T>(m, 0)) {}
    Matrix(int n) : A(n, vector<T>(n, 0)) {}
    int height() const {
        return (A.size());
    }
    int width() const {
        return (A[0].size());
    }
    const vector<T> &operator[](int k) const {
        return (A[k]);
    }
    vector<T> &operator[](int k) {
        return (A[k]);
    }
    static Matrix I(int n) {
        Matrix mat(n);
        for (int i = 0; i < n; i++) mat[i][i] = 1;
        return mat;
    }
    Matrix &operator+=(const Matrix &B) {
        int n = height(), m = width();
        assert(n == B.height() && m == B.width());
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                (*this)[i][j] += B[i][j];
            }
        }
        return (*this);
    }
    Matrix &operator-=(const Matrix &B) {
        int n = height(), m = width();
        assert(n == B.height() && m == B.width());
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                (*this)[i][j] -= B[i][j];
            }
        }
        return (*this);
    }
    Matrix &operator*=(const Matrix &B) {
        int n = height(), m = B.width(), p = width();
        assert(p == B.height());
        vector<vector<T>> C(n, vector<T>(m, 0));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                for (int k = 0; k < p; k++) {
                    C[i][j] += (*this)[i][k] * B[k][j];
                }
            }
        }
        A.swap(C);
        return (*this);
    }
    Matrix &operator^=(long long k) {
        int n = height();
        assert(n == width());
        Matrix B = Matrix::I(n);
        while (k > 0) {
            if (k & 1) B *= *this;
            *this *= *this;
            k >>= 1LL;
        }
        A.swap(B.A);
        return (*this);
    }
    Matrix operator+(const Matrix &B) const {
        return (Matrix(*this) += B);
    }
    Matrix operator-(const Matrix &B) const {
        return (Matrix(*this) -= B);
    }
    Matrix operator*(const Matrix &B) const {
        return (Matrix(*this) *= B);
    }
    Matrix operator^(const long long k) const {
        return (Matrix(*this) ^= k);
    }
};

#include<atcoder/modint>
using mint=atcoder::modint1000000007;

const int X0[7][7]={
    {1,1,0,2,0,1,1},
    {0,1,0,0,0,0,0},
    {0,0,1,1,0,1,1},
    {0,0,0,1,0,0,0},
    {0,0,0,0,1,1,1},
    {0,0,0,0,0,1,0},
    {0,0,0,0,0,0,1}
};

const int X1[7][7]={
    {1,0,0,0,0,0,0},
    {1,1,2,0,1,0,1},
    {0,0,1,0,0,0,0},
    {0,0,1,1,1,0,1},
    {0,0,0,0,1,0,0},
    {0,0,0,0,1,1,1},
    {0,0,0,0,0,0,1}
};

void solve(){
    string T;
    cin>>T;
    ll K;
    cin>>K;
    ll N=T.size();
    Matrix<mint>m0(7,7),m1(7,7);
    rep(i,0,7)rep(j,0,7){
        m0[i][j]=X0[i][j];
        m1[i][j]=X1[i][j];
    }
    Matrix<mint>mat(7,7);
    rep(i,0,7)mat[i][i]=1;
    rep(i,0,N){
        if(T[i]=='0')mat*=m0;
        else mat*=m1;
    }
    mat^=K;
    mint ans=mat[0][6]+mat[1][6];
    cout<<ans.val()<<"\n";
}

int main() {
    solve();
}
0