hdu5950

题意

\(给出 f_1 , f_2 ,以及递推式 f_n = 2 * f_{n-2} + f_{n-1} + n^4 ,求 f_n (mod=2147493647)\)

推导一下。

\[\begin{Bmatrix}
f_n\\
f_{n-1}\\
f_{n-2}\\
(n+1)^4\\
(n+1)^3\\
(n+1)^2\\
(n+1)\\
1
\end{Bmatrix} =
\begin{Bmatrix}
1 & 2 & 0 & 1 & 0 & 0 & 0 & 0\\
1 & 0 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0\\
0 & 0 & 0 & 1 & 4 & 6 & 4 & 1\\
0 & 0 & 0 & 0 & 1 & 3 & 3 & 1\\
0 & 0 & 0 & 0 & 0 & 1 & 2 & 1\\
0 & 0 & 0 & 0 & 0 & 0 & 1 & 1\\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 1
\end{Bmatrix} *
\begin{Bmatrix}
f_{n-1}\\
f_{n-2}\\
f_{n-3}\\
n^4\\
n^3\\
n^2\\
n\\
1
\end{Bmatrix}\]

矩阵快速幂即可。

code

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std; const ll MOD = 2147493647;
const int SIZE = 11;
ll n;
//定义结构体
struct Matrix
{
ll mat[SIZE][SIZE];
Matrix()
{
memset(mat, 0, sizeof mat);
}
};
//矩阵乘法 重载 * 操作符
Matrix operator * (Matrix a, Matrix b)
{
Matrix c;
memset(c.mat, 0, sizeof(c.mat));
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
for(int k = 0; k < SIZE; k++)
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
}
}
return c;
}
//矩阵快速幂 重载 ^ 操作符
Matrix operator ^ (Matrix a, ll k)
{
Matrix t;
memset(t.mat, 0, sizeof(t.mat));
for(int i = 0; i < SIZE; i++) // 单位矩阵
t.mat[i][i] = 1;
while(k)
{
if(k & 1)
t = t * a;
a = a * a;
k >>= 1;
}
return t;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
Matrix mt;
ll a, b;
scanf("%lld%lld%lld", &n, &a, &b);
ll c = 2 * a + b + 81;
c %= MOD;
if(n == 1) printf("%lld\n", a % MOD);
else if(n == 2) printf("%lld\n", b % MOD);
else if(n == 3) printf("%lld\n", c % MOD);
else
{
mt.mat[0][0] = 1;
mt.mat[0][1] = 2;
mt.mat[0][3] = 1;
mt.mat[1][0] = 1;
mt.mat[2][1] = 1;
mt.mat[3][3] = 1;
mt.mat[3][4] = 4;
mt.mat[3][5] = 6;
mt.mat[3][6] = 4;
mt.mat[3][7] = 1;
mt.mat[4][4] = 1;
mt.mat[4][5] = 3;
mt.mat[4][6] = 3;
mt.mat[4][7] = 1;
mt.mat[5][5] = 1;
mt.mat[5][6] = 2;
mt.mat[5][7] = 1;
mt.mat[6][6] = 1;
mt.mat[6][7] = 1;
mt.mat[7][7] = 1;
mt = mt ^ (n - 3);
ll ans = mt.mat[0][0] * c + mt.mat[0][1] * b + mt.mat[0][2] * a + mt.mat[0][3] * 256
+ mt.mat[0][4] * 64 + mt.mat[0][5] * 16 + mt.mat[0][6] * 4 + mt.mat[0][7];
printf("%lld\n", ans % MOD);
}
}
return 0;
}

最新文章

  1. CRL 版本2.2.0.0发布
  2. 自定义控件之圆形的image
  3. app之间的跳转,进入二级界面
  4. 设置文件为源文件(和src一样)
  5. mfc 在VC的两个对话框类中传递参数的三种方法
  6. zw版_zw中文增强版Halcon官方Delphi例程
  7. ls Common Command-Line Options
  8. hdu Fibonacci
  9. C语言日期时间标准库
  10. 转:.Net程序员学习Linux最简单的方法
  11. 1615: [Usaco2008 Mar]The Loathesome Hay Baler麻烦的干草打包机
  12. Mac 多个JDK的版本 脚本切换
  13. JS多个对象添加到一个对象中
  14. Java多线程:synchronized的可重入性
  15. tensorflow分类-【老鱼学tensorflow】
  16. 如何明确区分代码中的1和l
  17. OneNote中添加代码问题
  18. 20175325 《JAVA程序设计》实验二《JAVA开发环境的熟悉》实验报告
  19. [MySQL]理解关系型数据库4个事务隔离级别
  20. zabbix 监控java通用

热门文章

  1. 内嵌的Component调用外部的方法
  2. Angular2.js——数据显示
  3. hdu5145 NPY and girls
  4. DirectFB 之 字体显示(2)
  5. mui开发app之多图压缩与上传(仿qq空间说说发表)
  6. .net 程序集
  7. [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
  8. 区别client、offset、scroll系列以及event的几个距离属性
  9. 回答集编程背景(Answer Set Programming)
  10. redis安装学习