非010串

如果一个01字符串满足不存在010这样的子串,那么称它为非010串。
求长度为n的非010串的个数。(对1e9+7取模)
Input
一个数n,表示长度。(n<1e15)
Output
长度为n的非010串的个数。(对1e9+7取模)
Input示例
3
Output示例
7

解释:
000
001
011
100
101
110
111
#include<iostream>
#include<vector>
#include<cstring>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
const int masn=2e5+10;
typedef long long LL;
const int mod=1e9+7;
typedef struct Matrix {
    LL mat[4][4];
    Matrix() {
        memset(mat,0,sizeof(mat));
    }
    Matrix operator*(const Matrix &m1) {
        Matrix m;
        for(int i=0; i<4; i++)
            for(int j=0; j<4; j++)
                for(int k=0; k<4; k++)
                    m.mat[i][j]=(m.mat[i][j]+mat[i][k]*m1.mat[k][j])%mod;
        return m;
    }
} Matrix;
void init(Matrix &t) {
    for(int i=0; i<4; i++)
        t.mat[i][i]=1;
}
Matrix ans(LL n,Matrix a) {
    Matrix t;
    init(t);
    while(n) {
        if(n&1) t=t*a;
        a=a*a;
        n>>=1;
    }
    return t;
}
int main() {
    LL t;
    Matrix m,p;
    m.mat[0][0]=m.mat[0][1]=m.mat[0][3]=m.mat[1][0]=m.mat[2][1]=m.mat[3][2]=1;
    p.mat[0][0] = 12,p.mat[1][0] = 7,p.mat[2][0] = 4,p.mat[3][0] = 2;
    while(scanf("%lld",&t)!=EOF) {
        if(t<5) cout<<p.mat[4-t][0]<<endl;
        else  cout<<(ans(t-4,m)*p).mat[0][0]<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值