题目链接:https://vjudge.net/problem/HDU-5950

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2727    Accepted Submission(s): 1226

Problem Description
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 i4. 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 < 231 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*1十2十3^4.
In the second case, the third number is 93 = 2*1十1*10十3^4 and the fourth number is 369 = 2 * 10 十 93 十 4^4.

 
Source
 
Recommend
jiangzijing2015
 

题意:

求 f(n) = f(n−1) + 2*f(n−2) + n^4,其中 f(1)=a,f(2)=b

题解:

典型的矩阵快速幂的运用。关键是i^4怎么维护?我们可以当成求第i+1项,那么i^4就变成了(i+1)^4。那么这时我们可以用二项式定理从i^4、i^3、i^2、i^1、i^0的组合中得到(i+1)^4。也就是说总共需要维护:f[i+1]、f[i]、(i+1)^4、(i+1)^3、(i+1)^2、(i+1)^1、(i+1)^0。矩阵如下:

代码如下:

 #include <bits/stdc++.h>
#define rep(i,s,t) for(int (i)=(s); (i)<=(t); (i)++)
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const LL mod = ;
const int maxn = 1e5; struct Mat
{
LL mat[][];
void init()
{
rep(i,,) rep(j,,)
mat[i][j] = (i==j);
}
}; Mat p = { , , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , , , , ,
, , , ,, ,
}; Mat mul(Mat x, Mat y)
{
Mat s;
ms(s.mat,);
rep(i,,) rep(j,,) rep(k,,)
s.mat[i][j] += (x.mat[i][k]*y.mat[k][j])%mod, s.mat[i][j] %= mod;
return s;
} Mat qpow(Mat x, LL y)
{
Mat s;
s.init();
while(y)
{
if(y&) s = mul(s, x);
x = mul(x, x);
y >>= ;
}
return s;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
LL n, a, b;
scanf("%lld%lld%lld",&n,&a,&b);
if(n == )
{
printf("%lld\n",a);
continue;
}
if(n == )
{
printf("%lld\n",b);
continue;
} Mat x = p;
x = qpow(x, n-); LL ans = ;
ans = (ans + b*x.mat[][]) % mod;
ans = (ans + a*x.mat[][]%mod) % mod;
ans = (ans + *x.mat[][]%mod) % mod;
ans = (ans + *x.mat[][]%mod) % mod;
ans = (ans + *x.mat[][]%mod) % mod;
ans = (ans + *x.mat[][]%mod) % mod;
ans = (ans+x.mat[][]) % mod;
printf("%lld\n",ans);
}
}

HDU5950 Recursive sequence —— 矩阵快速幂的更多相关文章

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

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

  2. HDU5950 Recursive sequence (矩阵快速幂)

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

  3. 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 ...

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

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

  5. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  6. hdu 5950 Recursive sequence 矩阵快速幂

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

  7. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  8. UVA - 10689 Yet another Number Sequence 矩阵快速幂

                      Yet another Number Sequence Let’s define another number sequence, given by the foll ...

  9. Yet Another Number Sequence——[矩阵快速幂]

    Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...

随机推荐

  1. "从客户端中检测到有潜在危险的 Request.Form 值"的解决方案汇总

    #事故现场 在一个asp.net 的项目中,前端通过ajax将富文本中的文字内容post到服务端的一个ashx中,在ashx中尝试读取参数值时, 结果报错:“从客户端中检测到有潜在危险的 Reques ...

  2. 从源码解析 Spring JDBC 异常抽象

    初入学习 JDBC 操作数据库,想必大家都写过下面的代码: 数据库为:H2 如果需要处理特定 SQL 异常,比如 SQL 语句错误,这个时候我们应该怎么办? 查看 SQLException 源码,我们 ...

  3. 使用JAXP对xml文档进行DOM解析基础

    XML解析方式分为两种:dom和sax         dom:(Document Object Model, 即文档对象模型) 是 W3C 组织推荐的处理 XML 的一种方式.       sax: ...

  4. numpy常用函数学习

    目录numpy常用函数学习点乘法线型预测线性拟合裁剪.压缩和累乘相关性多项式拟合提取符号数组杂项点乘法该方法为数学方法,但是在numpy使用的时候略坑.numpy的点乘为a.dot(b)或numpy. ...

  5. Java中泛型的Class<Object>与Class<?>的区别(转)

    Object是一个具体的类名,而?是一个占位符号,表示任何类型,只要是SomeClass类或者子类就可以. List<Object>可以放任何类对象. List<? extends ...

  6. nginx 自签名https

    繁杂的命令,以下准备写好的sh,拷贝https.sh文件,设置执行权限:chmod u+x https.sh #!/bin/sh # create self-signed server certifi ...

  7. 4.【nuxt起步】-具体练习一个h5实例

    目标地址:https://www.vyuan8.com/vyuan/plugin.php?id=vyuan_fangchan&module=fangchan&pid=10079& ...

  8. HBase 基本操作

    如何添加列族 很简单,跟rdbms一样 直接用alter,但是alter之前必须先disable这个表 ---->disable 'test'                          ...

  9. Git 的使用Git Bash和Git GUI

    使用Github也有一年的时间了,之前一直都是使用的Github客户端,对提交,更新,克隆,合并,分支有一定的了解和实践.一直都想试试命令行的形式,但是感觉可能桌面版的方便就没有做. 可是Github ...

  10. Opencv 最小外接矩形合并拼接

    前一篇画出了最小外接矩形,但是有时候画出来的矩形由于中间像素干扰或者是其他原因矩形框并不是真正想要的 如图1是一个信号的雨图,被矩形框分割成了多个小框: 需要合并矩形框达到的效果: 主要思想: 扫描两 ...