您的位置:首页 > 产品设计 > UI/UE

HDU 5950 Recursive sequence(矩阵快速幂)

2016-11-07 19:46 477 查看

Recursive sequence

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 437 Accepted Submission(s): 220

[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
3 1 2
4 1 10


[align=left]Sample Output[/align]

85
369
HintIn 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.


[align=left]Source[/align]
2016ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

题意:已知f(i)=2f(i-2)+f(i-1)+i的4次方。给出f(1)和f(2)和n,求f(n)mod2147493647。
思路:一眼扫过去就觉得应该用矩阵快速幂,然而因为有i的4次方所以一开始放弃了这个思路转而去推公式。然而最终还是用矩阵快速幂,不过要构造一个7x7的矩阵来解决,很巧妙。只要把矩阵构造出来,其他的直接套模板即可。

#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
long long mod=2147493647UL;

struct matrix{
long long m[7][7];
}E,A;

void init(){
for(int i=0;i<7;i++){
for(int j=0;j<7;j++)
E.m[i][j]=(i==j);
}
}

matrix mul(matrix A,matrix B){
matrix ans;
for(int i=0;i<7;i++){
for(int j=0;j<7;j++){
ans.m[i][j]=0;
for(int k=0;k<7;k++){
ans.m[i][j]+=(A.m[i][k]%mod)*(B.m[k][j]%mod)%mod;
}
}
}
return ans;
}

matrix quickmod(matrix A,int n){
matrix t=A,d=E;
while(n>0){
if(n&1)
d=mul(d,t);
n>>=1;
t=mul(t,t);
}
return d;
}

int main(){

int t;
scanf("%d",&t);
while(t--){
long long a,b,n;
scanf("%lld %lld %lld",&n,&a,&b);
if(n==1)
printf("%lld\n",a);
else if(n==2){
printf("%lld\n",b);
}
else{
init();
A.m[0][0]=1;A.m[0][1]=2;A.m[0][2]=1;A.m[0][3]=4;A.m[0][4]=6;A.m[0][5]=4;A.m[0][6]=1;
A.m[1][0]=1;A.m[1][1]=0;A.m[1][2]=0;A.m[1][3]=0;A.m[1][4]=0;A.m[1][5]=0;A.m[1][6]=0;
A.m[2][0]=0;A.m[2][1]=0;A.m[2][2]=1;A.m[2][3]=4;A.m[2][4]=6;A.m[2][5]=4;A.m[2][6]=1;
A.m[3][0]=0;A.m[3][1]=0;A.m[3][2]=0;A.m[3][3]=1;A.m[3][4]=3;A.m[3][5]=3;A.m[3][6]=1;
A.m[4][0]=0;A.m[4][1]=0;A.m[4][2]=0;A.m[4][3]=0;A.m[4][4]=1;A.m[4][5]=2;A.m[4][6]=1;
A.m[5][0]=0;A.m[5][1]=0;A.m[5][2]=0;A.m[5][3]=0;A.m[5][4]=0;A.m[5][5]=1;A.m[5][6]=1;
A.m[6][0]=0;A.m[6][1]=0;A.m[6][2]=0;A.m[6][3]=0;A.m[6][4]=0;A.m[6][5]=0;A.m[6][6]=1;
matrix ans=quickmod(A,n-2);
long long sum=(ans.m[0][0]*b%mod+ans.m[0][1]*a%mod+(ans.m[0][2]*16)%mod+(ans.m[0][3]*8)%mod+(ans.m[0][4]*4)%mod+
ans.m[0][5]*2%mod+ans.m[0][6]%mod)%mod;
printf("%lld\n",sum);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: