Recursive sequence

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

InputThe 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 < 231231 as described above. 
OutputFor 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.

矩阵快速幂。利用了矩阵合并将两个递推关系合并到一个矩阵中。
之前做过了不少含有变量项的题,这道题是底数为变量,指数为常数的一种。
其中变量项的递推利用了二项式定理,系数满足杨辉三角规律。
 
#include <bits/stdc++.h>
#define MAX 10
#define MOD 2147493647
using namespace std;
typedef long long ll; struct mat{
ll a[MAX][MAX];
}; mat operator *(mat x,mat y)
{
mat ans;
memset(ans.a,,sizeof(ans.a));
for(int i=;i<=;i++){
for(int j=;j<=;j++){
for(int k=;k<=;k++){
ans.a[i][j]+=(x.a[i][k]*y.a[k][j]+MOD)%MOD;
ans.a[i][j]%=MOD;
}
}
}
return ans;
}
mat qMod(mat a,ll n)
{
ll tt[][]={,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,,
,,,,,,,};
mat t;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
t.a[i][j]=tt[i][j];
}
}
while(n){
if(n&) a=t*a;
n>>=;
t=t*t;
}
return a;
}
int main()
{
int t,i,j;
ll n,a,b;
scanf("%d",&t);
while(t--){
scanf("%I64d%I64d%I64d",&n,&a,&b);
if(n<){
if(n==) printf("%I64d\n",a);
if(n==) printf("%I64d\n",b);
continue;
}
mat x;
memset(x.a,,sizeof(x.a));
x.a[][]=b;
x.a[][]=a;
x.a[][]=***;
x.a[][]=**;
x.a[][]=*;
x.a[][]=;
x.a[][]=;
x=qMod(x,n-);
printf("%I64d\n",x.a[][]);
}
return ;
}
 

HDU - 5950 Recursive sequence(二项式+矩阵合并+矩阵快速幂)的更多相关文章

  1. HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

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

  2. HDU 5950 Recursive sequence 递推转矩阵

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

  3. hdu 5950 Recursive sequence 递推式 矩阵快速幂

    题目链接 题意 给定\(c_0,c_1,求c_n(c_0,c_1,n\lt 2^{31})\),递推公式为 \[c_i=c_{i-1}+2c_{i-2}+i^4\] 思路 参考 将递推式改写\[\be ...

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

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

    题目链接:Recursive sequence 题意:给出前两项和递推式,求第n项的值. 题解:递推式为:$F[i]=F[i-1]+2*f[i-2]+i^4$ 主要问题是$i^4$处理,容易想到用矩阵 ...

  6. hdu 5950 Recursive sequence 矩阵快速幂

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

  7. HDU 5950 Recursive sequence(矩阵快速幂)题解

    思路:一开始不会n^4的推导,原来是要找n和n-1的关系,这道题的MOD是long long 的,矩阵具体如下所示 最近自己总是很坑啊,代码都瞎吉坝写,一个long long的输入写成%d一直判我TL ...

  8. hdu 5950 Recursive sequence

    题意:告诉你数列的递推公式为f(n+1)=f(n)+2*f(n-1)+(n+1)^4 以及前两项a,b:问第n项为多少,结果对2147493647取模. 题解:有递推公式,马上应该就能想到矩阵快速幂: ...

  9. Luogu 3390 【模板】矩阵快速幂 (矩阵乘法,快速幂)

    Luogu 3390 [模板]矩阵快速幂 (矩阵乘法,快速幂) Description 给定n*n的矩阵A,求A^k Input 第一行,n,k 第2至n+1行,每行n个数,第i+1行第j个数表示矩阵 ...

随机推荐

  1. Python爬虫-- selenium库

    selenium库 selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行处理(S ...

  2. 使用阿里云maven镜像加速jar包下载

    编辑 MAVEN_HOME/conf 文件夹下的 settings.xml,找到 <mirrors> 节点,把下面内容添加在其子节点内: <mirror> <id> ...

  3. Rocketmq消息持久化

    本文编写,参考:https://my.oschina.net/bieber/blog/725646 producer Send()的Message最终将由broker处理,处理类为:SendMessa ...

  4. jqueryeasyUI dialog 弹出窗口超出浏览器,导致不能关闭的bug解决方案

    jqueryeasyUI dialog 弹出窗口超出浏览器,导致不能关闭的bug解决方案 2014年8月30日 3233次浏览 相信很多前端朋友都用过jqueryeasyUI,jqueryeasyUI ...

  5. <JAVA8新增内容>关于集合的操作(Collection/Iterator/Stream)

    因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterato ...

  6. Cocos2d-x中判断点击是否在触摸屏区域

    新建2dx工程. 在HelloWorld头文件加入以下语句: virtual void registerWithTouchDispatcher();//注册触屏事件 覆写register方法 virt ...

  7. mvn使用记录

    1. mvn dependency:copy-dependencies 会导出到targed/dependency 下面 2. mvn dependency:copy-dependencies -Do ...

  8. Go丨语言对数据库操作报错 panic: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine actively refused it.

    panic: dial tcp 127.0.0.1:3306: connectex: No connection could be made because the target machine ac ...

  9. Go丨语言学习笔记--switch

    Java语言与Go语言的switch对比 Go语言 switch str { case "yes" : do something ... case "no" d ...

  10. leetcode 23. Merge k Sorted Lists(堆||分治法)

    Merge k sorted linked lists and return it as one sorted list. 题意:把k个已经排好序的链表整合到一个链表中,并且这个链表是排了序的. 题解 ...