hdu 5950 Recursive sequence
Posted vividbingo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 5950 Recursive sequence相关的知识,希望对你有一定的参考价值。
Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5120 Accepted Submission(s): 2197
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 i^4. 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 <2^31 as described above.
Each case contains only one line with three numbers N, a and b where N,a,b <2^31 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
题意:T组数据。每组数据有N,F[1]=a,F[2]=b,F[N]=2*F[N-1]+F[N-2]+n^4,求F[N]。
题解:矩阵构造。先把
处理一下,
,完成了由
到
的转换。利用F[1],F[2],
,
,
,
,
构造矩阵。
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/1f9513644add4bcaa78ba9e56920fa8d.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/1f9513644add4bcaa78ba9e56920fa8d.jpg%3D%28n-1+1%29%5E%7B4%7D%3DC_%7B4%7D%5E%7B0%7D%28n-1%29%5E%7B4%7D+C_%7B4%7D%5E%7B1%7D%28n-1%29%5E%7B3%7D+C_%7B4%7D%5E%7B2%7D%28n-1%29%5E%7B2%7D+C_%7B4%7D%5E%7B3%7D%28n-1%29%5E%7B1%7D+C_%7B4%7D%5E%7B4%7D%28n-1%29%5E%7B0%7D)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/1f9513644add4bcaa78ba9e56920fa8d.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/f4d264cbb0784d4da63b7a0b842c4735.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/1f9513644add4bcaa78ba9e56920fa8d.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/48e6920625114a7585e6faf2bbf221d6.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/b47718c310044d5db8ef356aac9089a5.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/87501ff70a0e4b03b0da48b7dae2179a.jpg)
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program. 技术图片](https://image.cha138.com/20210714/a631899266484e8fa0121bae3f61606f.jpg)
![技术图片](https://image.cha138.com/20210714/2ac86c0b2f07462f88d77d182bb348dd.jpg)
代码:
#include<bits/stdc++.h>
using namespace std;
const long long mod=2147493647;
struct node
long long Martix[7][7];
node operator *(const node&n) const
int i,j,k;node sum;
for(i=0;i<7;i++)
for(j=0;j<7;j++)
sum.Martix[i][j]=0;
for(k=0;k<7;k++)
sum.Martix[i][j]=(sum.Martix[i][j]+Martix[i][k]*n.Martix[k][j])%mod;
return sum;
;
int main()
long long T,n,a,b;
node A,B,ans;
scanf("%ld",&T);
while(T--)
scanf("%lld%lld%lld",&n,&a,&b);
fill(A.Martix[0],A.Martix[0]+49,0);
fill(B.Martix[0],B.Martix[0]+49,0);
fill(ans.Martix[0],ans.Martix[0]+49,0);
for(int i=0;i<7;i++) A.Martix[i][i]=B.Martix[i][i]=1;
ans.Martix[0][0]=2*a%mod;ans.Martix[0][1]=b%mod;
ans.Martix[0][2]=81;ans.Martix[0][3]=27;
ans.Martix[0][4]=9;ans.Martix[0][5]=3;ans.Martix[0][6]=1;
B.Martix[0][0]=0;B.Martix[0][1]=1;B.Martix[1][0]=B.Martix[5][4]=2;
B.Martix[2][1]=B.Martix[6][2]=B.Martix[6][3]=B.Martix[6][4]=B.Martix[6][5]=1;
B.Martix[3][2]=B.Martix[5][2]=4;
B.Martix[4][2]=6;
B.Martix[4][3]=B.Martix[5][3]=3;
n-=2;
while(n)
if(n&1) A=A*B;
n>>=1;
B=B*B;
ans=ans*A;
printf("%lld\\n",ans.Martix[0][1]%mod);
system("pause");
return 0;
以上是关于hdu 5950 Recursive sequence的主要内容,如果未能解决你的问题,请参考以下文章
Recursive sequence(HDU5950 矩阵快速幂)
HDu 5950 Recursive sequence(矩阵快速幂)
HDU 5950 Recursive sequence 矩阵快速幂
HDU5950-Recursive sequence(矩阵快速幂)
HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]