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. 九度OJ 1068:球的半径和体积 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5385 解决:1949 题目描述: 输入球的中心点和球上某一点的坐标,计算球的半径和体积 输入: 球的中心点和球上某一点的坐标,以如下形式输 ...

  2. 双缓冲类里的OnPaint与OnSize,以及构造函数的关系

    代码摘自wx\lib\agw\knobctrl.py一点体会是,OnSize作为class的函数,被放在构造函数里执行,会先于OnPaint执行.测试结果是,初始启动后,会执行8次OnSize(为什么 ...

  3. ES5中的类与继承

    最近在重新复习TypeScript,看到类这块的时候自然会和ES5中的类写法进行对比加深印象. 发现ES5的类与继承一些细节还是挺多的,时间久了容易忘记,特此记录下. 首先是ES5的类定义,这没什么好 ...

  4. 【题解】CF264B Good Sequences

    [题解]CF264B Good Sequences 具有很明显的无后效性. 考虑\(dp\). 考虑初始条件,显然是\(dp(0)=0\) 考虑转移,显然是\(dp(t)=max(dp[k])+1\) ...

  5. install_driver(mysql) failed

        安装好了mysql监控神器innotop,正得意,innotoop不可用,其错误提示为install_driver(mysql) failed: Can't load '/usr/lib64/ ...

  6. ul和li 基本用法分析(这里主要想学习怎么用在导航栏中)

    常用作:导航,少量数据表格,居中 一.做导航,居中 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  7. LeetCode:二叉树剪枝【814】

    LeetCode:二叉树剪枝[814] 题目描述 给定二叉树根结点 root ,此外树的每个结点的值要么是 0,要么是 1. 返回移除了所有不包含 1 的子树的原二叉树. ( 节点 X 的子树为 X ...

  8. BZOJ1833 数位DP

    数位DP随便搞搞. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstrin ...

  9. linux字符设备学习笔记【原创】

    1.申请设备号 int register_chrdev_region(dev_t from, unsigned count, const char *name) 指定从设备号from开始,申请coun ...

  10. 笔记 jsp/ajax/js/jquery/html5/css+div->table

    1. jsp 1).jsp(39,33)   equal symbol expected: 这个异常是说第39行有 " '( 冒号单引号)问题 2)${map[key]}  map和key换 ...