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. windowsphone8.1学习笔记之应用数据(三)

    之前说了如何操作文本文件,如果是图片文件或者其他的二进制文件则需要操作文件的Stream或者Buffer数据.就需要用到DataReader和DataWriter这两个类了,这个的好好的练一下,以后的 ...

  2. 九度OJ 1144:Freckles(斑点) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1538 解决:760 题目描述: In an episode of the Dick Van Dyke show, little Richi ...

  3. 开发过程中,本地分支和远程跟踪分支发生了diverge

    1 git基本概念梳理 1.1 git的工作目录.暂存区和HEAD指向的版本库以及branch的概念 一个branch就是整个产品的一套代码,而工作目录中就是存放的本branch最新的代码,HEAD指 ...

  4. (1)Web 应用是一个状态机,视图与状态是一一对应的。 (2)所有的状态,保存在一个对象里面。

    Redux 入门教程(一):基本用法 - 阮一峰的网络日志 http://www.ruanyifeng.com/blog/2016/09/redux_tutorial_part_one_basic_u ...

  5. 我的Android进阶之旅------>android视频播放只有声音无画面的解决办法

    今天调试公司用VideoView实现的播放器来播放视频的时候,只有声音输出而无画面输出.一开始以为是自己程序有问题,调试了半天无果.怀疑是真机本身的问题,于是下了几个第三方的播放器来进行视频播放,例如 ...

  6. 关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法

    文章转载自:关于调用notifyDataSetChanged刷新PullToRefreshListView列表无反应解决办法 | TeachCourse

  7. 常用BAPI list

    2017-03-25 MD 主数据 1.创建物料主数据 CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' 2.创建供应商 CALL METHOD VMD_EI_API=&g ...

  8. ME51N, ME52N 创建采购申请的一个BADI

    ME51N ME52N创建修改采购申请时的一个BADI (2013-07-11 16:50:58) 转载▼ 标签: 采购申请 me51n me52 badi me_process_req_cust 分 ...

  9. 51Nod 1515 明辨是非 —— 并查集 + 启发式合并

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 1515 明辨是非  题目来源: 原创 基准时间限制:1 ...

  10. 51nod 1022 石子归并 V2 —— DP四边形不等式优化

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1022 1022 石子归并 V2  基准时间限制:1 秒 空间限 ...