题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950

Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers a and b on a blackboard. And then, the cows would say their identity number one by one. The first cow says the first number a and the second says the second number b. After that, the i-th cow says the sum of twice the (i-2)-th number, the (i-1)-th number, and $i^4$. Now, you need to write a program to calculate the number of the N-th cow in order to check if John’s cows can make it right.

Input
The first line of input contains an integer t, the number of test cases. t test cases follow. 
Each case contains only one line with three numbers N, a and b where $N,a,b < 2^31$ as described above.

Output
For each test case, output the number of the N-th cow. This number might be very large, so you need to output it modulo $2147493647$.

Sample Input
2
3 1 2
4 1 10

Sample Output
85
369

Hint
In the first case, the third number is $85 = 2 \times 1 + 2 + 3^4$.
In the second case, the third number is $93 = 2 \times 1 + 1 \times 10 + 3^4$ and the fourth number is $369 = 2 \times 10 + 93 + 4^4$.

题意:

给出 $a,b,n$,已知 $a_1 = a, a_2 = b$,且对于 $i>2$ 的 $a_i = 2a_{i-2} + a_{i-1} + i^4$,求 $a_n$。

题解:

看一眼 $n$ 最大在 $2e9$,显然不是暴力的递推。考虑矩阵快速幂加速递推。

不妨设一个矩阵

那么就有

我们只要求出一个矩阵 $A$,使其满足 $F(n+1) = F(n) \times A$,就能进行矩阵快速幂加速递推。

根据二项式定理很容易得到

因此不难就求得满足 $F(n+1) = F(n) \times A$ 矩阵 $A$ 如下:

此时,对于任意的 $F(n)$,都可以由 $F(n) = F(2) \times A^{n-2}$ 求得,$A^{n-2}$ 用矩阵快速幂可以 $O(\log n)$ 求出。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=; const int DIM=;
struct Matrix
{
ll mat[DIM][DIM];
Matrix operator*(Matrix const &oth)const
{
Matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<DIM;i++)
for(int j=;j<DIM;j++)
for(int k=;k<DIM;k++)
res.mat[i][j]+=(mat[i][k]*oth.mat[k][j])%mod,
res.mat[i][j]%=mod;
return res;
}
}A,F2,Fn;
Matrix fpow(Matrix base,ll n)
{
Matrix res;
memset(res.mat,,sizeof(res.mat));
for(int i=;i<DIM;i++) res.mat[i][i]=;
while(n)
{
if(n&) res=res*base;
base=base*base;
n>>=;
}
return res;
} void initA()
{
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=, A.mat[][]=;
} ll n,a,b;
int main()
{
ios::sync_with_stdio();
cin.tie(); initA();
int T;
cin>>T;
while(T--)
{
cin>>n>>a>>b;
if(n==) {
cout<<a<<'\n';
continue;
}
if(n==) {
cout<<b<<'\n';
continue;
} memset(F2.mat,,sizeof(F2.mat));
memset(Fn.mat,,sizeof(Fn.mat)); F2.mat[][]=a, F2.mat[][]=b,
F2.mat[][]=***, F2.mat[][]=**, F2.mat[][]=*, F2.mat[][]=, F2.mat[][]=; Fn=F2*fpow(A,n-);
cout<<Fn.mat[][]<<'\n';
}
}

HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]的更多相关文章

  1. HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)

    题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...

  2. hdu 5950 Recursive sequence 矩阵快速幂

    Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  3. HDU 1757 矩阵快速幂加速递推

    题意: 已知: 当x<10时:f(x)=x 否则:f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + --+ a9 * f(x-10); 求:f(x ...

  4. CH 3401 - 石头游戏 - [矩阵快速幂加速递推]

    题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...

  5. 5950 Recursive sequence (矩阵快速幂)

    题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...

  6. 洛谷P1357 花园(状态压缩 + 矩阵快速幂加速递推)

    题目链接:传送门 题目: 题目描述 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(<=N<=^).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻 ...

  7. [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)

    Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...

  8. CH3401 石头游戏(矩阵快速幂加速递推)

    题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...

  9. [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)

    Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...

随机推荐

  1. ClassicFTP for Mac(FTP 客户端)破解版安装

    1.软件简介    ClassicFTP 是 macOS 系统上一款易于使用的 FTP 客户端,让您能够从远程服务器(网站)或网络查看,编辑,上传,下载和删除文件的免费的软件.Mac 下的一款使用 F ...

  2. SpringMVC(Springboot)返回文件方法

    https://blog.csdn.net/Lynn_coder/article/details/79953977 ****************************************** ...

  3. npm国内镜像

    国内使用默认的源安装较慢,镜像使用方法(三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候配置还在): 1.通过config命令 npm config set registry h ...

  4. Linux下rar 命令压缩和解压详解

    例1:添加文件或目录到压缩档案中,使用a命令.例如把文件files1添加到abc.rar中,使用a或m命令,a命令把file1文件添加到abc.rar档案中保持原有的file1文件不变,m命令移动fi ...

  5. 红米3 MoKee 7.1.2_r36 自编译版/去魔趣中心、宙斯盾/息屏禁止刷新UI 2018年5月5日更新

    一.ROM简介 MoKee是基于CM二次修改的ROM,本地化系统:农历.归属地.OMS框架.状态栏显示网速/时间显秒等等. 二.ROM自编译DIY简介 1.Lawnchair桌面. 2.Via谷歌版浏 ...

  6. openssl 非对称加密DSA,RSA区别与使用介绍

    在日常系统管理工作中,需要作一些加解密的工作,通过openssl工具包就能完成我们很多需求! 1. openssl RSA 加解密 RSA是基于数论中大素数的乘积难分解理论上的非对称加密法,使用公私钥 ...

  7. mininet下建立拓扑时关于远程控制器的一个小问题

    最近重装了系统和mininet后,使用mininet时遇到了一点小问题,一开始忽视了细节,使得自己被这个问题困扰了好一会儿,好在后来还是发现了问题所在,故记录下来. $ sudo mn --topo ...

  8. 关于Unity中的Mesh Collider碰撞器

    原来我的场景中有一个平面Plane带Mesh Collider碰撞器组件,一个主角Hero带有一个Box Collider碰撞器和有重力的Rigidbody刚体组件,主角可以放在平面上. 在导入场景后 ...

  9. msyql同步的时候报错 : 错误代码: 1293 Incorrect table definition;there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause

    场景,两个不同服务器上的数据库,进行数据库同步 但是执行之后,提示报错 错误代码: 1293 Incorrect table definition; there can be only one TIM ...

  10. Java ThreadPool的正确打开方式花钱的年华 | 江南白衣(5星推荐)

    线程池应对于突然增大.来不及处理的请求,无非两种应对方式: 将未完成的请求放在队列里等待 临时增加处理线程,等高峰回落后再结束临时线程 JDK的Executors.newFixedPool() 和ne ...