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. Asp.Net Mvc: 浅析TempData机制

    一. Asp.Net Mvc中的TempData 在Asp.Net Mvc框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...

  2. android菜鸟学习笔记9----Activity(二)

    关于Activity的生命周期: 下面是Activity整个生命周期中,状态发生变化时所回调的方法,它们对应着Activity完整的生命过程. void  onCreate(Bundle savedI ...

  3. AWS:3. S3

    主要内容 1.S3入门 2.S3安全性 对象 权限 访问策略 3.S3实战--BAAS 应用与定价 S3入门 S3概念 S3是simple storge server简单存储服务 相当于网盘,例如百度 ...

  4. 点聚-weboffice 6.0 (二)

    1.修订操作 //设置当前操作用户 function SetUserName() { try{ var webObj=document.getElementById("WebOffice1& ...

  5. 我的Android进阶之旅------>Android如何通过自定义SeekBar来实现视频播放进度条

    首先来看一下效果图,如下所示: 其中进度条如下: 接下来说一说我的思路,上面的进度拖动条有自定义的Thumb,在Thumb正上方有一个PopupWindow窗口,窗口里面显示当前的播放时间.在Seek ...

  6. 蓝牙 CTS 测试

    安装蓝牙测试安装包 之后  . 安卓包名字 android-cts-6.0_r19-linux_x86-x86.zip 解压之后 /cts/android-cts/tools/  目录下 运行  ./ ...

  7. div中p标签自动换行

    只需要设置div的width属性,p标签加上word-break:break-word属性就会自动换行 ----------------2016.7.1-------------------- 今天在 ...

  8. "flash download failed - Target dll has been cancelled"错误解决办法

    在用mdk通过stlink烧写官方例程到stm32f429I discovery时,烧写了十来个程序都没问题,突然在烧写一个程序时, 弹出了“flash download failed - Targe ...

  9. php 获取上上个月数据 使用 strtotime('-1 months')的一个bug

    今天,使用php 日期函数处理数据,发现一个问题. 具体场景是这样的,我一直以为strtotime  格式化当前日期 或 指定日期可以找到对应的数据,比如我要查找上上个与的数据,因为我要获取当前时间的 ...

  10. tensorflow学习官网地址

    摘自: http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/overview.html 内容很多,需要花时间看完