如果一个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; }