非010串
来源:互联网 发布:微商天下软件 编辑:程序博客网 时间:2024/05/17 04:10
如果一个01字符串满足不存在010这样的子串,那么称它为非010串。
求长度为n的非010串的个数。(对1e9+7取模)
Input
一个数n,表示长度。(n<1e15)
Output
长度为n的非010串的个数。(对1e9+7取模)
Input示例
3
Output示例
7解释:000001011100101110111#include <iostream>#include <cstring>using namespace std;const long long int MOD = 1e9+7;struct matrix{long long int val[4][4];void clean(){memset(val, 0, sizeof(val));}void init(){memset(val, 0, sizeof(val));for (int i = 0; i < 4; i++){val[i][i] = 1;}}matrix operator *(const matrix &a){matrix result;result.clean();for (int i = 0; i < 4; i++){for (int j = 0; j < 4; j++){for (int k = 0; k < 4; k++){result.val[i][j] += val[i][k]*a.val[k][j];result.val[i][j] %= MOD;}}}return result;}};matrix fun(long long int n, matrix &base){matrix result;result.init();while (n > 0){if (n & 1){result = result * base;}base = base * base;n >>= 1;}return result;}int main(){ long long int n; cin >> n; matrix base = { 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0};matrix result;result.clean();result.val[0][0] = 7;result.val[1][0] = 4;result.val[2][0] = 2;result.val[3][0] = 1;if (n <= 3){cout << result.val[3-n][0] << endl;}else{base = fun(n-3, base);result = base * result;cout << result.val[0][0] << endl;} return 0;}
阅读全文
0 0
- 非010串
- 非010串
- 51NOD 非010串
- [51nod1668]非010串
- 算法马拉松18-B-非010串
- 51nod 1668 非010串
- 非
- !非
- 【51Nod算法马拉松18 B】非010串
- 51nod1668非010串+矩阵快速幂
- 51nod1668 非010串(矩阵快速幂)
- 51nod 1668 非010串(矩阵快速幂)
- 51nod 非010串(dp找规律+矩阵快速幂)
- acdream 1773 最长非回文串
- AcDream 1773 最长非回文串
- ACdream 1773 最长非回文串
- (Java)判断回文串,忽略既非字母又非数字的字符
- 串的模式匹配算法(非kmp)
- Android—常用控件(二)按钮
- Hibernate-入门 idea + maven构建
- datasnap连接池使用
- Android—常用控件(三)选择框
- 图像尺寸与分辨率
- 非010串
- 安装wamp,提示丢失msvcr110.dll文件 ,安装好后出现 403 forbidden问题的解决
- Kotlin基础教程-在kotlin中调用Java
- 如何把用户从所在的用户组(非主组)中删除
- 浅谈死锁
- Android—常用控件(四)日期/时间控件
- 再补充两种web攻击
- Nginx下使Thinkphp URL模式支持PATHINFO和REWRITE
- C语言-预处理[笔记]