传送门
题目描述
如果一个01字符串满足不存在010这样的子串,那么称它为非010串。
求长度为n的非010串的个数。(对1e9+7取模)
分析
d p dp dp思路应该比较好想,然构造矩阵跑一下矩阵快速幂即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e6 + 10;
const ll mod = 1e9 + 7;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
struct Node {
ll a[10][10];
Node() {
memset(a, 0, sizeof a);
}
} A;
int idx = 4;
Node operator*(const Node &A, const Node &B) {
Node res;
for (int i = 1; i <= idx; i++)
for (int j = 1; j <= idx; j++)
for (int k = 1; k <= idx; k++)
res.a[i][j] = (res.a[i][j] + A.a[i][k] * B.a[k][j]) % mod;
return res;
}
Node fastm(Node A, ll x) {
Node res;
for (int i = 1; i <= idx; i++) res.a[i][i] = 1;
while (x) {
if (x & 1) res = res * A;
A = A * A;
x >>= 1;
}
return res;
}
int main() {
ll n;
read(n);
if (n == 1) {
di(2);
return 0;
}
if (n == 2) {
di(4);
return 0;
}
A.a[1][2] = A.a[2][2] = A.a[2][3] = A.a[3][1] = A.a[3][4] = A.a[4][1] = A.a[4][4] = 1;
A = fastm(A, n - 2);
ll res = 0;
for (int i = 1; i <= 4; i++)
for (int j = 1; j <= 4; j++)
res = (res + A.a[i][j]) % mod;
dl(res);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/