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. Android开发学习--MVP模式入门

    1.模型与视图完全分离,我们可以修改视图而不影响模型2.可以更高效地使用模型,因为所有的交互都发生在一个地方——Presenter内部3.我们可以将一个Presenter用于多个视图,而不需要改变Pr ...

  2. Redis和SpringDataRedis

    一.Redis简介 ​ Redis是用C语言开发的一个开源的高性能键值对(key-value)数据库,运行在内存中,由ANSI C编写.企业开发通常采用Redis来实现缓存.同类的产品还有memcac ...

  3. IPython notebook快捷键(Jupyter notebook)

    转自“https://blog.csdn.net/eswai/article/details/53642802” 本文整理了神器IPython Notebook(或Jupyter Notebook)的 ...

  4. thinkphp5 404 file_put_contents 无法打开流:权限被拒绝

    如果你用TP的时间比较长,或者说你比较了解TP的人都会知道,TP的runtime它需要的权限是很大的,如果你只给一般权限肯定是不行的,通常都是给runtime权限:777: linux命令如下: cd ...

  5. pjax

    下载地址: https://github.com/defunkt/jquery-pjax/find/master 使用方法: 0.先引入jquery和jquery.pjax.js 1.父页面定义区域d ...

  6. codeforces_C. Sequence Transformation

    http://codeforces.com/contest/1059/problem/C 题意: 最初给一个1.2.3.…….n的序列,每次操作先将所有元素的最大公约数加入答案序列,然后在序列中任意删 ...

  7. css 样式渲染

     1.文字过长时,自动换行

  8. autorun - 自动装载/卸载CDROMs并在装载后执行/path/to/cdrom/autorun

    总览 autorun [-lmqv?V] [-a EXEC] [-c CDPLAYER] [-e STRING] [-i MILLISEC] [-n STRING] [-t STRING] [--au ...

  9. confluence的安装

    参考链接:https://www.ilanni.com/?p=11989 一.什么是confluence confluence是一个专业的企业知识管理与协同软件,可以用于构建企业wiki.通过它可以实 ...

  10. CAD参数绘制角度标注(网页版)

    主要用到函数说明: _DMxDrawX::DrawDimAngular 绘制一个角度标注.详细说明如下: 参数 说明 DOUBLE dAngleVertexX 角度标注的顶点的X值 DOUBLE dA ...