Recursive sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2882    Accepted Submission(s): 1284

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.

 
题意     f[n]=f[n-1]+2*f[n-2]+n^4; f[1]=a f[2]=b   求第n项
解析     直接递推肯定会超时的   所以 构造一个7*7系数矩阵 直接快速幂解出来  由于现在比较菜 只会最简单的矩阵 勉强可以写出来。。。。
 
1 2 1 0 0 0 0         f[i-1]        f[i]
1 0 0 0 0 0 0         f[i-2]        f[i-1]  
0 0 1 4 6 4 1    i^4          (i+1)^4
0 0 0 1 3 3 1       *    i^3                  =            (i+1)^3
0 0 0 0 1 2 1    i^2          (i+1)^2
0 0 0 0 0 1 1    i            i+1
0 0 0 0 0 0 1      1           1  
 
AC代码
 #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=,maxn=;
struct Matrix
{
ll m[maxn][maxn];
Matrix()
{
memset(m,,sizeof(m));
}
void init()
{
for(int i=; i<maxn; i++)
for(int j=; j<maxn; j++)
m[i][j]=(i==j);
}
Matrix operator +(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
c.m[i][j]=(m[i][j]+b.m[i][j])%mod;
}
}
return c;
}
Matrix operator *(const Matrix &b)const
{
Matrix c;
for(int i=; i<maxn; i++)
{
for(int j=; j<maxn; j++)
{
for(int k=; k<maxn; k++)
{
c.m[i][j]=(c.m[i][j]+(m[i][k]*b.m[k][j])%mod)%mod;
}
}
}
return c;
}
Matrix operator^(const ll &t)const
{
Matrix ans,a=(*this);
ans.init();
ll n=t;
while(n)
{
if(n&) ans=ans*a;
a=a*a;
n>>=;
}
return ans;
}
};
int main()
{
int t;
ll n,m,a,b;
scanf("%d",&t);
while(t--)
{
scanf("%lld %lld %lld",&n,&a,&b);
if(n==)
{
cout<<a%mod<<endl;
continue;
}
if(n==)
{
cout<<b%mod<<endl;
continue;
}
Matrix temp;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=,temp.m[][]=;temp.m[][]=;
Matrix aa,bb;
bb.m[][]=b%mod;
bb.m[][]=a%mod;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
bb.m[][]=;
aa=temp^(n-);
aa=aa*bb;
cout<<aa.m[][]%mod<<endl;
}
return ;
}
    

HDU 5950 Recursive sequence 递推转矩阵的更多相关文章

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

  2. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

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

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

  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 Farmer John likes to play mathematics games with his N cows. Recently, they are a ...

  7. hdu 5950 Recursive sequence 矩阵快速幂

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

  8. hdu 5860 Death Sequence(递推+脑洞)

    Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historian liv ...

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

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

随机推荐

  1. Spring数据访问2 - 通过JDBC访问数据库

    因为原生的jdbc操作太复杂,几乎都是建立连接.关闭连接和处理例外等模板式的代码,Spring对此进行了抽象——使用模板来消除样板式代码 ,JdbcTemplate承担了简化数据库访问这块的任务. 利 ...

  2. iOS定位--CoreLocation框架

    CoreLocation框架的使用 // 首先导入头文件 #import <CoreLocation/CoreLocation.h> CoreLocation框架中所有数据类型的前缀都是C ...

  3. 契约式设计(DbC)感想(二)

    契约式设计6大原则的理解 在<Design by Contract原则与实践>中,作者定义了契约式设计的6大原则: 区分命令和查询: 将基本查询和派生查询区分开: 针对每个派生查询,设定一 ...

  4. 迅为双核imx6DL核心板_ARM定制专家_Cortex SATA 千兆网 4G GPS

    核心板参数 尺寸:51mm*61mm CPU:Freescale Cortex-A9 双核精简版 i.MX6DL,主频 1.2 GHz 内存:1GB DDR3 存储:8GB EMMC 存储 EEPRO ...

  5. 迅为i.MX6Q嵌入式开发板

    工业级核心板:核心板10层高速PCB设计,充分保证电磁兼容. 01. 处理器:开发板默认是四核商业扩展级芯片,可根据用户需求更换单核.双核.工业级.汽车级处理器,批量更省成本. 02. 扩展引脚:32 ...

  6. python_函数的可变参数

    def test(*args,**kwargs): print(args) print(kwargs) test(1,2,3,x=1,y=2) 运行结果: *args称为positional argu ...

  7. python3安装opencv及电子书籍(百度云)

    不能直接  pip install opencv 正解: pip install opencv-python  记得:请确保网络良好!!!!! (1)这个是我学习的电子书籍:opencv-python ...

  8. 如何优雅地从CSDN转载文章

    复制粘贴应该是最显而易见的方法,但是不仅会有丢失内容,而且格式也会丢失.要想达到更好的效果,可以从html源码入手. 1.在chrome浏览器中打开要转载的文章,右键选择检查 2.在chrome的右方 ...

  9. log4net小记

    log4net添加: Install-Package Log4net log4net.config配置: <?xml version="1.0" encoding=" ...

  10. iOS缓存到内存

    前面一片文章介绍了如何上传和下载文件,这篇文章将介绍一下如何在iOS设备中进行缓存. 这篇文章将只介绍一下将内容缓存到内存中,下一篇文章就介绍一下在iOS磁盘上缓存内容. 使用缓存的目的是为了使用的应 ...