题目链接

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. Linux每天一个命令:iostat

    iostat用于输出CPU和磁盘I/O相关的统计信息 安装Sysstat工具包 centos: yum install sysstat ubuntu: sudo apt-get install sys ...

  2. Vue.js 系列教程 2:组件,Props,Slots

    原文:intro-to-vue-2-components-props-slots 译者:nzbin 这是关于 JavaScript 框架 Vue.js 五个教程的第二部分.在这一部分,我们将学习组件, ...

  3. Python股票分析系列——数据整合.p7

    欢迎来到Python for Finance教程系列的第7部分. 在之前的教程中,我们为整个标准普尔500强公司抓取了雅虎财经数据. 在本教程中,我们将把这些数据组合到一个DataFrame中. 到此 ...

  4. oracle 11g空表不能exp导出问题解决方案

    oracle 11g空表不能exp导出问题解决方案 最近由于要进行迁移服务器代码和数据库,突然发现导出的表少了,通过排查发现空表尽然没有exp导出,真是郁闷啊,虽然是空表没数据,但也不能没有啊,如何是 ...

  5. 第四次oo博客

    论述测试与正确性论证的效果差异 单元测试利用测试者构造的测试用例来检查类或方法的正确性,一般来说所需要测试的用例是无穷多的,通过人为构造代表性的测试用例来尽量测试所有代码.测试的优点在于不易出错,只要 ...

  6. Python_列表推导式_生成器的表达式_各种推导式_40

    列表推导式: #列表推导式: egg_list = [] for i in range(10): egg_list.append('鸡蛋%s'%i) print(egg_list) egon egg_ ...

  7. 多项式求值 n维多项式 Horner解法

    #include<iostream> using namespace std; template<class T> T ploy(T *coeff,int n,const T& ...

  8. HDU - 1540 线段树的合并

    这个题题意我大概解释一下,就是一开始一条直线,上面的点全是联通的,有三种操作 1.操作D把从左往右第x个村庄摧毁,然后断开两边的联通. 2.询问Q节点相联通的最长长度 3.把最后破坏的村庄重建. 这个 ...

  9. [iOS]一行代码集成空白页面占位图(基于runtime+MJRefresh思想)

    2018年01月03日阅读 2472   [iOS]一行代码集成空白页面占位图(基于runtime+MJRefresh思想) LYEmptyView 此框架是本人在5,6个月前,公司启动新项目的时候, ...

  10. VO和DO转换(四) MapStruct

    VO和DO转换(一) 工具汇总 VO和DO转换(二) BeanUtils VO和DO转换(三) Dozer VO和DO转换(四) MapStruct MapStruct