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 ...
随机推荐
- 云主机IO性能测试
1:数据读取速度 ucloud云主机最低224.8MB/S,最高508.8MB/S,平均410.7MB/S 阿里云主机最低17.4MB/S, 最高189.6MB/S,平均170.6MB/S ...
- oracle排序后的第一条记录
该查寻语句没有经过任何的优化,因为oracle没有SQL的TOP关键字,但是有一个ROWNUM的列,因此,可以通过ROWNUM来进行查询.oracle的关于rownum的参考手册里面提到了 分析 ...
- Android webview clearHistory 不符合逾期的解决办法
目前在业务开发中有这么一个需求,切换不同的 Fragment, 切换回 WebView 的Fragment时候,要求是打开的初始页面,然后我在 onHiddenChanged() 方法中加载默认地址, ...
- Socket网络编程--聊天程序(6)
这一小节将增加一个用户的结构体,用于保存用户的用户名和密码,然后发给服务器,然后在服务器进行判断验证.这里就有一个问题,以前讲的就是发送字符串是使用char类型进行传输,然后在服务器进行用同样是字符串 ...
- FOR XML PATH 可以将查询结果根据行输出成XML格式
SELECT CAST(OrderID AS varchar)+',' as OrderNo FROM Product CAST函数用于将某种数据类型的表达式显式转换为另一种数据类型 SELECT C ...
- python中关于round函数的小坑
这个一直都想写,但是因为这个点比较小,所以一直懒得动手.不过还是补上吧,留着早晚是个祸害. round函数很简单,对浮点数进行近似取值,保留几位小数.比如 >>> round(10. ...
- 【转】ubuntu16.04设置python3为默认及一些库的安装
原文:https://www.cnblogs.com/jokie/p/6933546.html Ubuntu默认Python为2.7,所以安装Python包时安装的为py2的包. 利用alternat ...
- zookeeper 入门(一)
1 下载安装 wget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.6.tar.gzcp zookeeper-3. ...
- NLog使用
NLog的配置文件,文件上面有详细的备注,注意这个配置文件一定要放在NLog.dll的文件夹里 <?xml version="1.0" encoding="utf- ...
- .Net MVC Cache 缓存技术总结
一.细说 ASP.NET Cache 及其高级用法 二..Net环境下的缓存技术介绍 (转) 三.asp.net中缓存的使用介绍一 四.HttpContext.Current.Cache 过期时间