結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

//入力が必ず-mod<a<modの時.
template<const int mod>
struct modint{ //mod変更が不可能.
    public:
    long long v = 0;
    static void setmod(int m){} //飾り.
    static constexpr long long getmod(){return mod;}
    modint(){v = 0;} modint(int a){v = a<0?a+mod:a;} modint(long long a){v = a<0?a+mod:a;}
    modint(unsigned int a){v = a;} modint(unsigned long long a){v = a;}
    long long val()const{return v;}
 
    modint &operator=(const modint &b) = default;
    modint operator+()const{return (*this);}
    modint operator-()const{return modint(0)-(*this);}
    modint operator+(const modint b)const{return modint(v)+=b;}
    modint operator-(const modint b)const{return modint(v)-=b;}
    modint operator*(const modint b)const{return modint(v)*=b;}
    modint operator/(const modint b)const{return modint(v)/=b;}
    modint operator+=(const modint b){
        v += b.v; if(v >= mod) v -= mod;
        return *this;
    }
    modint operator-=(const modint b){
        v -= b.v; if(v < 0) v += mod; 
        return *this;
    }   
    modint operator*=(const modint b){v = v*b.v%mod; return *this;}
    modint operator/=(modint b){ //b!=0 mod素数が必須.
        if(b == 0) assert(false);
        int left = mod-2;
        while(left){if(left&1) *this *= b; b *= b; left >>= 1;}
        return *this;
    }
    modint operator++(){*this += 1; return *this;}
    modint operator--(){*this -= 1; return *this;}
    modint operator++(int){*this += 1; return *this;}
    modint operator--(int){*this -= 1; return *this;}
    bool operator==(const modint b)const{return v == b.v;}
    bool operator!=(const modint b)const{return v != b.v;}
    bool operator>(const modint b)const{return v > b.v;}
    bool operator>=(const modint b)const{return v >= b.v;}
    bool operator<(const modint b)const{return v < b.v;}
    bool operator<=(const modint b)const{return v <= b.v;}
    modint pow(long long n)const{
        modint ret = 1,p = v;
        if(n < 0) p = p.inv(),n = -n;
        while(n){
            if(n&1) ret *= p;
            p *= p; n >>= 1;
        }
        return ret;
    }
    modint inv()const{return modint(1)/v;} //素数mod必須.
};
using mint = modint<1000000007>;

template<typename T>
vector<vector<T>> multima(vector<vector<T>> &A,vector<vector<T>> &B){
    assert(A.at(0).size() == B.size() && B.size());
    int H = A.size(),W = B.at(0).size();
    vector<vector<T>> ret(H,vector<T>(W));
    for(int i=0; i<H; i++) for(int k=0; k<W; k++){
        for(int l=0; l<A.at(i).size(); l++){
            ret.at(i).at(k) += A.at(i).at(l)*B.at(l).at(k);
        }
    }
    return ret; //計算できる行列のみ.
}
template<typename T>
vector<vector<T>> Powmat(vector<vector<T>> A,vector<vector<T>> B,long long N){ //return A^N*B ABは行列;
    //n*n行列A n*1行列Bと想定 O(n^3*logN).
    assert(A.size() && A.size() == A.at(0).size() && N >= 0);
    while(N){
        if(N&1) B = multima(A,B);
        A = multima(A,A); N >>= 1;
    }
    return B;
}

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

    string s; cin >> s;
    long long K; cin >> K;
    int N = s.size();

    vector<vector<mint>> zero = {
        {1,0,0,0,0,0,0},
        {1,1,1,0,0,0,0},
        {0,0,1,0,0,0,0},
        {2,0,2,1,1,0,0},
        {0,0,0,0,1,0,0},
        {1,0,1,0,1,1,1},
        {0,0,0,0,0,0,1}
    },
    one = {
        {1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0},
        {1,1,1,0,0,0,0},
        {0,0,0,1,0,0,0},
        {2,2,0,1,1,0,0},
        {0,0,0,0,0,1,0},
        {1,1,0,1,0,1,1}
    },all = {
        {1,0,0,0,0,0,0},
        {0,1,0,0,0,0,0},
        {0,0,1,0,0,0,0},
        {0,0,0,1,0,0,0},
        {0,0,0,0,1,0,0},
        {0,0,0,0,0,1,0},
        {0,0,0,0,0,0,1}
    };

    for(auto c : s){
        if(c == '0') all = multima(zero,all);
        else all = multima(one,all);
    }
    vector<vector<mint>> dp(7,vector<mint>(1));
    dp.at(0).at(0) = 1;
    dp = Powmat(all,dp,K);
   // for(int i=0; i<7; i++) cout << dp.at(i).at(0).v << " "; cout << endl;
    cout << (dp.at(6).at(0)+dp.at(5).at(0)).v << endl;

}
0