[luogu5004]专心OI - 跳房子【矩阵加速+动态规划】
传送门:https://www.luogu.org/problemnew/show/P5004
分析
动态规划转移方程是这样的\(f[i]=\sum^{i-m-1}_{j=0}f[j]\)。
那么很明显的是要构造举证,而且要维护前缀和,所以需要保留\(m+1\)项。
ac代码
#include <bits/stdc++.h>
#define ll long long
#define ms(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f
#define N 25
#define mod ((int)1e9 + 7)
using namespace std;
template <typename T>
inline void read(T &x) {
x = 0; T fl = 1;
char ch = 0;
while (ch < '0' || ch > '9') {
if (ch == '-') fl = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
x *= fl;
}
ll n;
int m;
struct Matrix {
int a[N][N], x, y;
void init() {
memset(a, 0, sizeof(a));
x = y = 0;
}
Matrix operator *(const Matrix &rhs) const{
Matrix res; res.init();
res.x = x, res.y = rhs.y;
int c = y;
for (int i = 1; i <= x; i ++) {
for (int j = 1; j <= y; j ++) {
for (int k = 1; k <= c; k ++) {
res.a[i][j] = (res.a[i][j] + (ll) a[i][k] * rhs.a[k][j]) % mod;
}
}
}
return res;
}
Matrix power(Matrix a, ll b) {
Matrix res;
res.init();
res.x = res.y = a.x;
for (int i = 1; i <= res.x; i ++) res.a[i][i] = 1;
for (; b; b >>= 1) {
if (b & 1) res = res * a;
a = a * a;
}
return res;
}
}a, b;
int main() {
read(n); read(m);
if (n <= m) {
printf("%lld\n", n + 1);
return 0;
}
a.x = a.y = m + 2, b.x = m + 2, b.y = 1;
for (int i = 2; i <= m + 2; i ++)
b.a[i][1] = 1;
b.a[1][1] = m + 1;
a.a[1][1] = a.a[1][2] = 1;
a.a[2][2] = a.a[2][m + 2] = 1;
for (int i = 3; i <= m + 2; i ++) a.a[i][i - 1] = 1;
a = a.power(a, n - m);
b = a * b;
printf("%d\n", b.a[1][1]);
return 0;
}
[luogu5004]专心OI - 跳房子【矩阵加速+动态规划】的更多相关文章
- Luogu-5004 专心OI-跳房子(矩阵快速幂)
Luogu-5004 专心OI-跳房子(矩阵快速幂) 题目链接 题解: 先考虑最朴素的dp 设\(f[i][0/1]\)表示第\(i\)个位置跳/不跳的方案数,则: \[ \begin{cases} ...
- 洛谷【P5004 专心OI - 跳房子】 题解
题目链接 https://www.luogu.org/problem/P5004 洛谷 P5004 专心OI - 跳房子 Imakf有一天参加了PINO 2017 PJ组,他突然看见最后一道题 他十分 ...
- 【LuoguP5004】 专心OI - 跳房子
首先这是一道计数类DP,那我们得先推式子,经过瞎掰乱凑,经过认真分析,我们可以得到这样的方程 F(N)=F(0)+F(1)+....+F(N-M-1) 所有F初值为1,F(1)=2 ANS=F(N+M ...
- 「P5004」专心OI - 跳房子 解题报告
题面 把\(N\)个无色格子排成一行,选若干个格子染成黑色,要求每个黑色格子之间至少间隔\(M\)个格子,求方案数 思路: 矩阵加速 根据题面,这一题似乎可以用递推 设第\(i\)个格子的编号为\(i ...
- ZZH与计数(矩阵加速,动态规划,记忆化搜索)
题面 因为出题人水平很高,所以这场比赛的题水平都很高. ZZH 喜欢计数. ZZH 有很多的数,经过统计,ZZH一共有 v 0 v_0 v0 个 0 , v 1 v_1 v1 个 1,-, v 2 ...
- HDU 5564 Clarke and digits 状压dp+矩阵加速
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5564 题意: 求长度在[L,R]范围,并且能整除7的整数的总数. 题解: 考虑最原始的想法: dp[ ...
- 【 CodeForces - 392C】 Yet Another Number Sequence (二项式展开+矩阵加速)
Yet Another Number Sequence Description Everyone knows what the Fibonacci sequence is. This sequence ...
- 【HDU 3483】 A Very Simple Problem (二项式展开+矩阵加速)
A Very Simple Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 【HDU3802】【降幂大法+矩阵加速+特征方程】Ipad,IPhone
Problem Description In ACM_DIY, there is one master called “Lost”. As we know he is a “-2Dai”, which ...
随机推荐
- CodeForces Round #550 Div.3
http://codeforces.com/contest/1144 A. Diverse Strings A string is called diverse if it contains cons ...
- Jeecg-Boot Spring Boot
Jeecg-Boot 1.0 发布,企业级快速开发平台 - 开源中国https://www.oschina.net/news/104889/jeecg-boot-1-0-released
- 设置永久环境变量linux
========================================================================== http://www.cnblogs.com/Bi ...
- 通过爬虫程序深入浅出java 主从工作模型
随手做的爬虫程序在 https://github.com/rli07/master_java/blob/master/spider.zip 可下载. 这是我做的系统学习图, 可以参考一下 系统架 ...
- Oracle创建表空间、用户以及给用户赋权
--创建表空间 create tablespace waterboss datafile 'd:\waterboss.dbf' size 100m autoextend on next 10m --创 ...
- 获取打开页面时的当前时间(yyyy-MM-dd hh:mm:ss)
Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...
- uname -r查询版本不是安装的版本的问题
uname -r 查出来的版本与/lib/modules下面的内核版本不匹配.啥原因? 第一步,先strace uname -r看看这个uname -r到底从哪里获取的版本. strace没有看出来 ...
- linux audit审计(4)--audit的日志切分,以及与rsyslog的切分协同使用
audit的规则配置稍微不当,就会短时间内产生大量日志,所以这个规则配置一定要当心.当audit日志写满后,可以看到如下场景: -r-------- 1 root root 8388609 Mar 3 ...
- jqprint控件使用
/*! jQuery v1.9.1 | (c) 2005, 2012 jQuery Foundation, Inc. | jquery.org/license //@ sourceMappingURL ...
- 扒一扒开源世界有哪些licenses?
摘要:license,中文译为“许可证”.在开源世界里,license是具有法律效力的,通过选择相应的license,版权拥有者可以声称自己相应的权利,包括其他人使用.修改.引用.共享等一系列涉及版权 ...