题目链接

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 (递推 矩阵快速幂优化)的更多相关文章

  1. hdu 2604 递推 矩阵快速幂

    HDU 2604 Queuing (递推+矩阵快速幂) 这位作者讲的不错,可以看看他的 #include <cstdio> #include <iostream> #inclu ...

  2. HDU 2842 (递推+矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2842 题目大意:棒子上套环.第i个环能拿下的条件是:第i-1个环在棒子上,前i-2个环不在棒子上.每个 ...

  3. hdu 6185 递推+矩阵快速幂

    思路:考虑全部铺满时,前2列的放法.有如下5种情况:(转自http://blog.csdn.net/elbadaernu/article/details/77825979 写的很详细 膜一下)  假设 ...

  4. 2018.10.09 NOIP模拟 路途(递推+矩阵快速幂优化)

    传送门 签到题.(考试的时候写挂爆0) 令AiA_iAi​表示邻接矩阵的iii次幂. 于是就是求Al+Al+1+...+ArA_l+A_{l+1}+...+A_rAl​+Al+1​+...+Ar​. ...

  5. HDU Queuing(递推+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  6. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  7. LightOJ 1244 - Tiles 猜递推+矩阵快速幂

    http://www.lightoj.com/volume_showproblem.php?problem=1244 题意:给出六种积木,不能旋转,翻转,问填充2XN的格子有几种方法.\(N < ...

  8. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  9. HDU - 6185 Covering(暴搜+递推+矩阵快速幂)

    Covering Bob's school has a big playground, boys and girls always play games here after school. To p ...

随机推荐

  1. 深入理解 ES6中的 Reflect

    阅读目录 一:Reflect.get(target, name, receiver) 二:Reflect.set(target,name,value,receiver) 三:Reflect.apply ...

  2. Android/Linux boot time分析优化

    如果需要优化boot time,就需要一个量化的工具来分析每个阶段的时间消耗.这种类型的优化特别适合使用基于timeline的图表,有着明显的时间顺序.要求不但能给出整个流程消耗的时间,还要能对流程进 ...

  3. git中Please enter a commit message to explain why this merge is necessary.

    Please enter a commit message to explain why this merge is necessary. 请输入提交消息来解释为什么这种合并是必要的 git 在pul ...

  4. 朱晔的互联网架构实践心得S1E10:数据的权衡和折腾【系列完】

    朱晔的互联网架构实践心得S1E10:数据的权衡和折腾[系列完] [下载本文PDF进行阅读] 本文站在数据的维度谈一下在架构设计中的一些方案对数据的权衡以及数据流转过程中的折腾这两个事情.最后进行系列文 ...

  5. layui轮播中箭头不起作用问题

    layui轮播中箭头不起作用问题 layui轮播插件在使用中发现箭头不起作用其他都合适,是什么原因造成的呢?发现单独提出layui中的demo是合适的,通过仔细慢慢的寻找,发现layui.use('c ...

  6. python 跨域处理方式

    因为浏览器的同源策略限制,不是同源的脚本不能操作其他源下面的资源,想操作另一个源下面的资源就属于跨域了,这里说的跨域是广义跨域,我们常说的代码中请求跨域,是狭义的跨域,即在脚本代码中向非同源域发送ht ...

  7. linux-高并发与负载均衡-TCP-IP基础知识

    ARP协议: ping baidu

  8. c++入门之初话指针

    先上代码:再进行总结知识: # include "iostream" struct ant_year_end { int year; }; int main() { using n ...

  9. Codeforces Round #534 (Div. 2)D. Game with modulo-1104-D(交互+二分+构造)

    D. Game with modulo time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. [python]解决Windows下安装第三方插件报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0:

    系统:win7IDE:pycharm Python版本:2.7 安装第三方插件是报错:  报错原因与编码有关,pip把下载的临时文件存放在了用户临时文件中,这个目录一般是C:\Users\用户名\Ap ...