Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)
F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3)
现在要求F[N]
类似于斐波那契数列的递推式子吧, 但是N最大能到int的最大值, 直接循环推解不了
所以就得用矩阵快速幂咯
现在就看转移矩阵长什么样了
Mi表示要求的矩阵 转移矩阵用A表示
A * Mi = Mi+1
矩阵Mi里面至少得有 F[i-1] F[i-2] i ^ 4 Mi+1就相应的有 F[i] F[i-1] (i+1)^4
(i+1)^4 = i^4 + 4 * i ^ 3 + 6 * i ^ 2 + 4 * i + 1
所以Mi中还得有i^3 i^2 i 1
总共就有七个元素
$\begin{pmatrix} 1 & 2 & 1 & 0 & 0 & 0 & 0 \\ 1 & 0& 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 1 & 4 & 6 & 4 & 1 \\ 0 & 0 & 0 & 1 & 3 & 3 & 1 \\ 0 & 0 & 0 & 0 & 1 & 2 & 1 \\ 0 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 0 & 0 & 0 & 0 & 0 & 1\end{pmatrix}
\times \begin{pmatrix} f_{i-1} \\ f_{i-2} \\ i^{4} \\ i^{3} \\ i^{2} \\ i \\ 1 \end{pmatrix} = \begin{pmatrix} f_{i} \\ f_{i-1} \\ (i+1)^{4} \\ (i+1)^{3} \\ (i+1)^{2} \\ i+1 \\ 1 \end{pmatrix}$
基本的矩阵运算,就是前面这个相当于系数的矩阵得是 (n-2)次幂 因为f1 f2都求过了
初始的矩阵是
$\begin{pmatrix} f_{2} \\ f_{1} \\ 3^{4} \\ 3^{3} \\ 3^{2} \\ 3 \\ 1 \end{pmatrix}$
也就是
$\begin{pmatrix} b \\ a \\ 81 \\ 27 \\ 9 \\ 3 \\ 1 \end{pmatrix}$
特判一下n == 1 和 2的情况就好啦
代码如下
#include <cstdio>
#define ll long long
#define MOD 2147493647
using namespace std; struct Matrix {
ll m[][];
Matrix() {
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
m[i][j] = ;
}
}; Matrix mul(Matrix A, Matrix B) {
Matrix temp;
for (int i = ; i < ; i++)
for (int j = ; j < ; j++)
for (int k = ; k < ; k++)
temp.m[i][j] = (temp.m[i][j] % MOD+ A.m[i][k] * B.m[k][j] % MOD) % MOD;
return temp;
} Matrix quick_mod(Matrix A, ll b) {
Matrix ans;
for (int i = ; i < ; i++)
ans.m[i][i] = ;
while (b) {
if (b & ) ans = mul(ans, A);
A = mul(A, A);
b >>= ;
}
return ans;
} int main() {
int T; scanf("%d", &T);
while (T--) {
int n, a, b;
scanf("%d%d%d", &n, &a, &b);
if (n == ) printf("%d\n", a);
else if (n == ) printf("%d\n", b);
else {
Matrix ans;
ans.m[][] = , ans.m[][] = , ans.m[][] = ;
ans.m[][] = ;
ans.m[][] = , ans.m[][] = , ans.m[][] = , ans.m[][] = , ans.m[][] = ;
ans.m[][] = , ans.m[][] = , ans.m[][] = , ans.m[][] = ;
ans.m[][] = , ans.m[][] = , ans.m[][] = ;
ans.m[][] = , ans.m[][] = , ans.m[][] = ;
ans = quick_mod(ans, (ll)n - );
Matrix temp;
temp.m[][] = b, temp.m[][] = a, temp.m[][] = , temp.m[][] = ;
temp.m[][] = , temp.m[][] = , temp.m[][] = ;
ans = mul(ans, temp);
printf("%lld\n", ans.m[][] % MOD);
}
}
return ;
}
其实可以封装一下Matrix 重载一下 * 和 ^ 运算符 这样就很方便也很好看啦
Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)的更多相关文章
- hdu 2604 递推 矩阵快速幂
HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...
- HDU 2842 (递推+矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...
- hdu 6185 递推+矩阵快速幂
思路:考虑全部铺满时,前2列的放法.有如下5种情况:(转自http://blog.csdn.net/elbadaernu/article/details/77825979 写的很详细 膜一下) 假设 ...
- 2018.10.09 NOIP模拟 路途(递推+矩阵快速幂优化)
传送门 签到题.(考试的时候写挂爆0) 令AiA_iAi表示邻接矩阵的iii次幂. 于是就是求Al+Al+1+...+ArA_l+A_{l+1}+...+A_rAl+Al+1+...+Ar. ...
- HDU Queuing(递推+矩阵快速幂)
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- LightOJ 1244 - Tiles 猜递推+矩阵快速幂
http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...
- [hdu 2604] Queuing 递推 矩阵快速幂
Problem Description Queues and Priority Queues are data structures which are known to most computer ...
- HDU - 6185 Covering(暴搜+递推+矩阵快速幂)
Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...
随机推荐
- Storm知识点
1. 离线计算是什么? 离线计算:批量获取数据.批量传输数据.周期性批量计算数据.数据展示 代表技术:Sqoop批量导入数据.HDFS批量存储数据.MapReduce批量计算数据.Hive批量计算数据 ...
- nginx 配sorry page - error page
include conf.d/*.conf; server { listen 9999; server_name 127.0.0.1; location / { root html; index er ...
- Linux下配置mysql远程访问
1 编辑mysql的配置文件 mysqld.cnf root@iZwz99xkrnh5xye3zgi4btZ:~# vi /etc/mysql/mysql.conf.d/mysqld.cnf 2 把 ...
- input表单提交完毕,返回重新填入有黄色背景,和 历史记录 清除
<input autocomplete="value"> // 添加这个属性,可以解决然后添加一个css input:-webkit-autofill {box-sha ...
- 深入理解Redis高可用方案-Sentinel
Redis Sentinel是Redis的高可用方案.是Redis 2.8中正式引入的. 在之前的主从复制方案中,如果主节点出现问题,需要手动将一个从节点升级为主节点,然后将其它从节点指向新的主节点, ...
- Centos7 下SVN迁移
SVN迁移需要做如下操作: 1. 将原来的Repository导出 . #svnadmin dump 原有repos的目录路径 > dumpfile (不同服务器安装目录不同,根据具体情况调整) ...
- Final Destination II -- 矩阵快速幂模板题
求f[n]=f[n-1]+f[n-2]+f[n-3] 我们知道 f[n] f[n-1] f[n-2] f[n-1] f[n-2] f[n-3] 1 1 ...
- Acceleration for ML 论文导读
Energy efficient parallel neuromorphic architectures with approximate arithmetic on FPGA Motivation ...
- hibernate添加数据时Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value: com.javakc.hibernate.test.entity.TestEntity.testName
意思是,一个非null属性引用了一个null或瞬态值.就是在对应实体类配置文件hbm.xml中该属性配置了not-null="true",将其去掉即可.
- 无法从带有索引像素格式的图像创建graphics对象
大家在用 .NET 做图片水印功能的时候, 很可能会遇到 “无法从带有索引像素格式的图像创建graphics对象”这个错误,对应的英文错误提示是“A Graphics object cannot be ...