hdu5950
hdu5950
题意
\(给出 f_1 , f_2 ,以及递推式 f_n = 2 * f_{n-2} + f_{n-1} + n^4 ,求 f_n (mod=2147493647)\)
推导一下。
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;
}
hdu5950的更多相关文章
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU5950(矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...
- 【HDU5950】Recursive sequence(矩阵快速幂)
BUPT2017 wintertraining(15) #6F 题意 \(f(1)=a,f(2)=b,f(i)=2*(f(i-2)+f(i-1)+i^4)\) 给定n,a,b ,\(N,a,b < ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence 非线性递推式 矩阵快速幂
题目传送门 题目描述:给出一个数列的第一项和第二项,计算第n项. 递推式是 f(n)=f(n-1)+2*f(n-2)+n^4. 由于n很大,所以肯定是矩阵快速幂的题目,但是矩阵快速幂只能解决线性的问题 ...
- HDU5950【矩阵快速幂】
主要还是i^4化成一个(i+1)^4没遇到过,还是很基础的一题矩阵快速幂: #include <bits/stdc++.h> using namespace std; typedef lo ...
- HDU5950 矩阵快速幂(巧妙的递推)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f[n] = 2*f[n-2] + f[n-1] + n^4 思路:对于递推题而言,如果递 ...
- RecursiveSequence(HDU-5950)【矩阵快速幂】
题目链接: 题意:Si=S(i-1)+2*S(i-2)+i^4,求Sn. 思路:想到了矩阵快速幂,实在没想出来怎么构造矩阵.... 首先构造一个向量vec={a,b,16,8,4,2,1}. 在构造求 ...
随机推荐
- js的break语句,continue语句,return语句
js的break语句,continue语句,return语句. 用的时候很容易混淆,有过一次泪奔的经历. break语句 break语句会使运行的程序立刻退出包含在最内层的循环或者退出一个switch ...
- 数据可视化之MarkPoint
MarkPoint是什么效果?如上图,一闪一闪亮晶晶的效果,这是在Echarts中对应的效果.我最早看到的是腾讯的一个Flash的版本,显示当前QQ在线人数的全国分布效果,感觉效果很炫,当时也在想,怎 ...
- Linux-进程描述(4)之进程优先级与进程创建执行
进程优先级 进程cpu资源分配就是指进程的优先权(priority).优先权高的进程有优先执行权利. 权限与优先级.权限(privilege)是指在多用户计算机系统的管理中,某个特定的用户具有特定的系 ...
- python自动化开发-[第一天]-基础数据类型与编码
1.Python与其他语言对比 - C语言的解释方式 代码-->机器码-->计算机 - python,java,php等高级语言的解释方式 代码-->字节码-->机器码-- ...
- 关于vs code的个人配置
vs code官方下载地址 : https://code.visualstudio.com/Download 下载好的vs code相当是一款纯文本编辑器,接下来开始进行对其配置: 页面设 ...
- PHP学习笔记-1
PHP基本语法 php脚本可以放在文档的任意位置: php脚本以<? php开始,以?>结束: php文件通常包括Html标签和一些php脚本代码: 举个栗子: <!DOCTYPE ...
- 编写一个简单的java服务器程序
import java.net.*;import java.io.*; public class server{ ); //监听在80端口 Socket sock = server.accept(); ...
- [UWP]了解模板化控件(9):UI指南
1. 使用TemplateSettings统一外观 TemplateSettings提供一组只读属性,用于在新建ControlTemplate时使用这些约定的属性. 譬如,修改HeaderedCont ...
- SDN学习之Mininet验证OpenFlow协议版本
最近学习如何使用mininet,但是,刚刚开始时一直无法知道如何查看OpenFlow协议的版本,通过查阅网上的资料,从SDNLAB中,学习到了如何验证,mininet自身基于OpenFlow13版本的 ...
- matlab笔记(1) 元胞结构cell2mat和num2cell
摘自于:https://zhidao.baidu.com/question/1987862234171281467.html https://www.zybang.com/question/dcb09 ...