【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 < 2^{31}\),求f(n)% 2147493647。
题解
i^4=(i-1)^4+4*(i-1)^3+6*(i-1)^2+4*(i-1)+1
\]
我们可以构造出矩阵乘法
\begin{matrix}
f_{i}\\
f_{i-1}\\
i^4\\
i^3\\
i^2\\
i\\
1\\
\end{matrix}
\right]
=
\left[
\begin{matrix}
1&2&1&4&6&4&1\\
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{matrix}
\right]
*
\left[
\begin{matrix}
f_{i-1}\\
f_{i-2}\\
(i-1)^4\\
(i-1)^3\\
(i-1)^2\\
i-1\\
1\\
\end{matrix}
\right]
\]
B为\([f_2,f_1,2^4,2^3,2^2,2,1]^T\)于是\(f(n)=A^{n-2}*B\)的第一项。
有了递推关系,再用矩阵快速幂解决就好了。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
#include <iostream>
using namespace std;
const ll mod=2147493647;
struct Mat{
int r,c;
ll a[10][10];
Mat(int _r,int _c){
r=_r;c=_c;
memset(a,0,sizeof a);
}
Mat operator *(const Mat &b)const{
Mat c(r,b.c);
for(int i=0;i<r;i++)
for(int j=0;j<b.c;j++)
for(int k=0;k<b.r;k++){
c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j]%mod)%mod;
}
return c;
}
}A(7,7),B(7,1);
Mat qpow(Mat a,int b){
Mat c(a.r,a.c);
for(int i=0;i<a.r;i++)c.a[i][i]=1;
while(b){
if(b&1)c=c*a;
b>>=1;
a=a*a;
}
return c;
}
int main() {
int at[10][10]={{1,2,1,4,6,4,1},
{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}};
for(int i=0;i<7;i++)for(int j=0;j<7;j++)A.a[i][j]=at[i][j];
int t,n,a,b;
cin>>t;
while(t--){
scanf("%d%d%d",&n,&a,&b);
B.a[0][0]=b;B.a[1][0]=a;
B.a[6][0]=1;
for(int i=5;i>1;i--)B.a[i][0]=B.a[i+1][0]*2;
if(n==1){
printf("%d\n",a);
}else if(n==2){
printf("%d\n",b);
}else{
Mat C=qpow(A,n-2)*B;
printf("%lld\n",C.a[0][0]);
}
}
return 0;
}
【HDU5950】Recursive sequence(矩阵快速幂)的更多相关文章
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence (矩阵快速幂)
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs
题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ i < k\\ ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
随机推荐
- Ordering Tasks
链接 [https://vjudge.net/contest/281085#problem/D] 题意 有n个任务,有M个对先后顺序 然你输出最后的完成任务的顺序,有多种可能输出一种即可 分析 裸的拓 ...
- NFV组播实验对照
一 论文题目:Approximation and Online Algorithms for NFV-Enabled Multicasting in SDNs 发表时间:2017 期刊来源:Inter ...
- python中读取文件的read、readline、readlines方法区别
#读取文件所有内容,返回字符串对象,python默认以文本方式读取文件,遇到结束符读取结束. fr = open('lenses.txt')read = fr.read()print(type(rea ...
- js中的一些方法
数组 //map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果. 返回值:一个新数组,每个元素都是回调函数的结果. var array1 = [1, 4, 9, ...
- h5-canvas(其他api)
###1.使用图片(需要image对象) drawImage(image,x,y,width,height) 其中image是image或者canvas对象,x和y 是其在目标canvas的起始坐标 ...
- Oracle 内存参数调优设置
Oracle 数据库系统中起到调节作用的参数叫初始化参数,数据库管理员根据实际情况需要适当调整这些 初始化参数以优化Oracle系统. 1 主要系统参数调优介绍 2 系统内存参数的分配 2.1 Ora ...
- Ajax发送请求等待时弹出模态框等待提示
主要的代码分为两块,一个是CSS定义模态框,另一个是在Ajax中弹出模态框. 查看菜鸟教程中的模态框教程demo,http://www.runoob.com/try/try.php?filename= ...
- 剑指offer(10)
题目: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 思路: 如果忽略题目中 ...
- Dart语法基础
hello world // Define a function. printNumber(num aNumber) { print('The number is $aNumber.'); // Pr ...
- 在linux系统中实现各项监控的关键技术(1)--cpu使用率的计算
转载自 Linux中通过/proc/stat等文件计算Cpu使用率 http://www.blogjava.net/fjzag/articles/317773.html proc文件系统 /proc文 ...