51NOD 非010串

32 篇文章 0 订阅
3 篇文章 0 订阅
010串
UsedToBe (命题人)
基准时间限制:1 秒 空间限制:131072 KB 分值: 80
如果一个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
d[i][k]    {k=0,1} 表示以k结尾 长度为i的串数量
d[i][0]=d[i-1][0]+d[i-2][1]   //在xxxxx0后+0 在xxxx1后+00
d[i][1]=d[i-1][0]+d[i-1][1]   //在xxxxx0后+1 在xxxxx1后+1
ans[i]=长度为i的串的数量
ans[i]=d[i][0]+d[i][1]
=2*d[i-1][0]+d[i-1][1]+d[i-2][1]
=ans[i-1]+d[i-1][0]+d[i-2][1]
=ans[i-1]+d[i-2][0]+d[i-2][1]+d[i-3][1]
=ans[i-1]+ans[i-2]+d[i-4][0]+d[i-4][1]
=ans[i-1]+ans[i-2]+ans[i-4]
so:ans[i]=ans[i-1]+ans[i-2]+ans[i-4]
然后矩阵快速幂即可 上队友代码
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)
#define MOD 1000000007

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ls rt<<1
#define rs rt<<1|1


typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 4;
using namespace std;
//#define TIME

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}

struct matrix{
    ll mat[maxn][maxn];
    void init(){
        mes(mat, 0);
        for(int i = 0; i < 4; ++i){
            mat[i][i] = 1;
        }
    }
    void clear(){
        mes(mat, 0);
    }
    void output(){
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                //  printf("%lld ", mat[i][j]);
                cout << mat[i][j] << " ";
            }
            printf("\n");
        }
    }
    matrix operator *(const matrix &base){
        matrix tmp;
        tmp.clear();
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                for(int k = 0; k < 4; ++k){
                    tmp.mat[i][j] = (tmp.mat[i][j] + mat[i][k]*base.mat[k][j]);
                    tmp.mat[i][j] %= MOD;
                }
            }
        }
        return tmp;
    }
};

matrix matrix_fast_mod(ll m, matrix base)
{
    matrix res;
    //  res.output();
    res.init();
    while(m){
        if(m&1){
            res = res*base;
        }
        base = base*base;
        m >>= 1;
    }
    return res;
}


int main()
{
    ll N;
    cin >> N;
    matrix base = {
        1, 1, 0, 1,
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0
    };
    matrix p;
    p.clear();
    p.mat[0][0] = 7;
    p.mat[1][0] = 4;
    p.mat[2][0] = 2;
    p.mat[3][0] = 1;
    if(N < 5){
        if(N < 4){
            cout << p.mat[3-N][0] << endl;
        }
        else{
            cout << "12" << endl;
        }
    }
    else{
        base = matrix_fast_mod(N-3, base);
        p = base*p;
        cout << p.mat[0][0] << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值