题目链接

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. Android自定义相机拍照并使用CardView展示

    直接上完整代码:在Android Studio新建一个项目,然后依次创建: 1.预先在drawable文件夹中保存的图片资源 2.创建:CameraPreView.java类: 3.创建:OnClic ...

  2. item 23: 理解std::move和std::forward

    本文翻译自<effective modern C++>,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 根据std::move和std::forward不 ...

  3. Item 21: 比起直接使用new优先使用std::make_unique和std::make_shared

    本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 让我们先从std::make_unique和std::make_s ...

  4. 朱晔和你聊Spring系列S1E4:灵活但不算好用的Spring MVC

    阅读PDF版本 本文会以一些例子来展现Spring MVC的常见功能和一些扩展点,然后我们来讨论一下Spring MVC好用不好用. 使用SpringBoot快速开始 基于之前的parent模块,我们 ...

  5. 线程GIL锁 线程队列 回调函数

    ----------------------------------无法改变风向,可以调整风帆;无法左右天气,可以调整心情.如果事情无法改变,那就去改变观念. # # ---------------- ...

  6. Windows下的两个缺陷

    记事本缺陷: 标题:新建记事本中仅输入“联通”,保存关闭后再打开,显示为乱码 详细描述: 环境说明:操作系统ALL 重现步骤: 1.新建一个记事本,在其中仅输入“联通”两个字 2.再将该记事本关闭保存 ...

  7. nginx 之 proxy_redirect详解

    proxy_redirect 语法:proxy_redirect [ default|off|redirect replacement ]  默认值:proxy_redirect default  使 ...

  8. matplotlib 入门之The Lifecycle of a plot

    文章目录 Note 数据 准备开始 操控风格 我错了!!! 定制图像 特别注意!!! figsize=(width, height)!!! 格式化标签 组合多个可视化对象? 保存你的图片 matplo ...

  9. 用C# BigInteger实现的BigDecimal类,终于可以直接做四则运算了。

    https://code.google.com/p/dotnet-big-decimal/ 这是个BigDecimal类的开源项目,支持Operators +, - and *. 俺给改了改,加上了除 ...

  10. 【转】Word之表格、图片的题注(抬头)自动编号

    问:word中的表格怎么自动插入题注(即表头的编号自动编号)? 答: 1首先搞清楚自动编号的意思.自动插入题注的意思是,在你在word中新建或者复制一个word表格的时候,表头的编号就自动生成了,而不 ...