题目链接: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. HtmlEmail实现简单发送邮件

    一般发送邮件的话系统项目中可能会用到,像一些通知信息自动发送等,会用到发送邮件的情况,发送邮件有好多种,包括设置各种格式,添加图片附件等,当然今天我们先看一下怎么实现发送成功. 工欲善其事必先利其器, ...

  2. SpringBoot中如何上传Upload

    [学习笔记] 5.上传:马克-to-win@马克java社区:根据第3部分的helloworld例子,用那个项目做底子.pom.xml都不用改变.参考项目bootUpload1.static/inde ...

  3. JDK1.8日期时间库学习

    这周在阅读<阿里巴巴Java开发手册>时,在并发处理一节的日期处理中,其强调SimpleDateFormat 是线程不安全的类,一般不要定义为 static 变量,如果 定义为 stati ...

  4. 【IntelliJ IDEA】idea导入项目只显示项目中的文件,不显示项目结构

    导入项目之后,只显示项目文件,不显示项目结构 解决方法 1.点击file->project structure..->Modules 点击右上角+加号 ->import Module ...

  5. 调用tf.softmax_cross_entropy_with_logits函数出错解决

    原来这个函数,不能按以前的方式进行调用了,只能使用命名参数的方式来调用.原来是这样的: tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y ...

  6. 学会用core dump调试程序错误

    最来在项目中遇到大型程序出现SIGSEGV ,一直不知道用core dump工具来调试程序,花了近一周的时间,才定位问题,老大很生气,后果很严重,呵呵,事后仔细学习了这块的知识,了解一点core du ...

  7. Eclipse 安装(Oxygen版本)

    Eclipse 安装(Oxygen版本) Eclipse 最新版本 Eclipse Neon,这个首次鼓励用户使用 Eclipse Installer 来做安装,这是一种由Eclipse Oomph提 ...

  8. vue2.0 自定义 侧滑删除(LeftSlider)组件

    1.自定义侧滑删除组件 LeftSlider.vue <!-- 侧滑删除 组件 --> <template> <div class="delete"& ...

  9. python(13)- 文件处理应用Ⅱ:增删改查

    用户选择1,增加功能: 用户输入www.oldboy2.org和server 11111 weight 2222 maxconn 3333后, 在www.oldboy2.org下增加一条server信 ...

  10. Allegro基本操作——PCB布线

    转:http://blog.sina.com.cn/s/blog_1538bc9470102vyyq.html http://www.elecfans.com/article/80/110/2010/ ...