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 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]的更多相关文章
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- hdu 5950 Recursive sequence 矩阵快速幂
Recursive sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 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 ...
- CH 3401 - 石头游戏 - [矩阵快速幂加速递推]
题目链接:传送门 描述石头游戏在一个 $n$ 行 $m$ 列 ($1 \le n,m \le 8$) 的网格上进行,每个格子对应一种操作序列,操作序列至多有 $10$ 种,分别用 $0 \sim 9$ ...
- 5950 Recursive sequence (矩阵快速幂)
题意:递推公式 Fn = Fn-1 + 2 * Fn-2 + n*n,让求 Fn; 析:很明显的矩阵快速幂,因为这个很像Fibonacci数列,所以我们考虑是矩阵,然后我们进行推公式,因为这样我们是无 ...
- 洛谷P1357 花园(状态压缩 + 矩阵快速幂加速递推)
题目链接:传送门 题目: 题目描述 小L有一座环形花园,沿花园的顺时针方向,他把各个花圃编号为1~N(<=N<=^).他的环形花园每天都会换一个新花样,但他的花园都不外乎一个规则,任意相邻 ...
- [bzoj1008](HNOI2008)越狱(矩阵快速幂加速递推)
Description 监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱 In ...
- CH3401 石头游戏(矩阵快速幂加速递推)
题目链接:传送门 题目: 石头游戏 0x30「数学知识」例题 描述 石头游戏在一个 n 行 m 列 (≤n,m≤) 的网格上进行,每个格子对应一种操作序列,操作序列至多有10种,分别用0~9这10个数 ...
- [bzoj1009](HNOI2008)GT考试 (kmp+矩阵快速幂加速递推)
Description 阿 申准备报名参加GT考试,准考证号为N位数X1X2....Xn(0<=Xi<=9),他不希望准考证号上出现不吉利的数字.他的不吉利数学 A1A2...Am(0&l ...
随机推荐
- AirServer for Mac(Airplay 终端实用工具)破解版安装
1.软件简介 AirServer 是一款 Mac 上的 AirPlay 终端,通过这款软件,利用 AirPlay 技术,iPhone 或 iPad 就可以无线连接到 Mac 上,不需要在 iPh ...
- javascript中的this在不同场景下的区别
javascript在初版的设计上存在失误,导致了这门语言在使用时,经验型写法并不能得到像其它几个流行语言一样预期.其中的this的使用就是一个典型. this在javascript中是由解释器注入的 ...
- pandas通过皮尔逊积矩线性相关系数(Pearson's r)计算数据相关性
皮尔逊积矩线性相关系数(Pearson's r)用于计算两组数组之间是否有线性关联,举个例子: a = pd.Series([1,2,3,4,5,6,7,8,9,10]) b = pd.Series( ...
- (4) MySQL中EXPLAIN执行计划分析
一. 执行计划能告诉我们什么? SQL如何使用索引 联接查询的执行顺序 查询扫描的数据函数 二. 执行计划中的内容 SQL执行计划的输出可能为多行,每一行代表对一个数据库对象的操作 1. ID列 ID ...
- 小米手机安装mitmproxy证书
[本文出自天外归云的博客园] 问题描述 小米手机在连接mitmproxy代理后通过浏览器访问mitm.it下载android证书后无法成功安装证书 设备:Redmi Note 2(红米手机) 解决方法 ...
- 【Spring源码分析】Bean加载流程概览(转)
转载自:https://www.cnblogs.com/xrq730/p/6285358.html 代码入口 之前写文章都会啰啰嗦嗦一大堆再开始,进入[Spring源码分析]这个板块就直接切入正题了. ...
- WARNING: Can not get binary dependencies for file...
环境: window7 64bit python 3.5 pyinstaller 3.2 用pyinstaller 将python文件打包成exe文件的过程中,出现了如下的错误 C:\Users\ca ...
- 在linux环境下编译android so库
(1) 配置Android NDK环境 (2) mk文件编写 LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) # OpenCV OPENCV_CA ...
- 蛋疼的mysql_ping()以及MYSQL_OPT_RECONNECT
From: https://www.felix021.com/blog/read.php?2102 昨天@Zind同学找到我之前的一篇blog(已经修改),里面提到了mysql_ping和MYSQL_ ...
- Oracle调整内存超出限制出现ORA-27100: shared memory realm already exists问题解决办法
今天测试服务器遇到问题 ORA-04030:out of process memory when trying to allocate string bytes 一看就猜到是内存不足了,把Oracle ...