Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 98 Accepted Submission(s): 46
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.
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
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.
题目大意:
求 f(n) = f(n−1)+2f∗(n−2)+n4 ,其中 f(1)=a,f(2)=b
解题思路:
首先一看这个题目,很容易想到构造矩阵:那么我们现在来分析一下怎么构造这个矩阵,那么
(n+1)4 = n4+4n3+6n2+4n+1
所以光
(n+1)4
这个矩阵就能构造出
5∗5
的一个矩阵来, 然后
f(n) = f(n−1)+2∗f(n−2)
这个是
2∗2
的矩阵,所以构造出来就应该是
7∗7
的转移矩阵
A
:
然后
f(n+1) = f(n)+2∗f(n−1)+(n+1)4
, 可得矩阵
A:
⎛⎝⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜⎜1214641100000000146410001331000012100000110000001⎞⎠⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟⎟
然后 f(n+1) = {{f(n),f(n−1),n4,n3,n2,n,1}∗An−2}.mat[0][0]
My Code:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef long long LL;
const int MAXN = 7;
const LL MOD = 2147493647;
typedef long long LL;
typedef struct
{
LL mat[MAXN][MAXN];
void Init(){
memset(mat, 0, sizeof(mat));
for(int i=0; i<MAXN; i++)
mat[i][i] = 1;
}
} Matrix;
///求得的矩阵
Matrix p = {1, 1, 0, 0, 0, 0, 0,
2, 0, 0, 0, 0, 0, 0,
1, 0, 1, 0, 0, 0, 0,
4, 0, 4, 1, 0, 0, 0,
6, 0, 6, 3, 1, 0, 0,
4, 0, 4, 3, 2, 1, 0,
1, 0, 1, 1, 1, 1, 1
};
///矩阵乘法
Matrix Mul_Matrix(Matrix a, Matrix b)
{
Matrix c;
for(int i=0; i<MAXN; i++)
{
for(int j=0; j<MAXN; j++)
{
c.mat[i][j] = 0;
for(int k=0; k<MAXN; k++)
{
c.mat[i][j] += (a.mat[i][k] * b.mat[k][j]) % MOD;
c.mat[i][j] %= MOD;
}
}
}
return c;
}
///矩阵的快速幂
Matrix quick_Mod_Matrix(LL m)
{
Matrix ans, b = p;
ans.Init();
while(m)
{
if(m & 1)
ans = Mul_Matrix(ans, b);
m>>=1;
b = Mul_Matrix(b, b);
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
LL n, a, b;
scanf("%lld%lld%lld",&n,&a,&b);
if(n == 1){
printf("%lld\n",a);
continue;
}
if(n == 2){
printf("%lld\n",b);
continue;
}
Matrix tmp = quick_Mod_Matrix(n-2);
LL ans = b*tmp.mat[0][0] % MOD;
ans = (ans + a*tmp.mat[1][0]%MOD) % MOD;
ans = (ans + 16*tmp.mat[2][0]%MOD) % MOD;
ans = (ans + 8*tmp.mat[3][0]%MOD) % MOD;
ans = (ans + 4*tmp.mat[4][0]%MOD) % MOD;
ans = (ans + 2*tmp.mat[5][0]%MOD) % MOD;
ans = (ans+tmp.mat[6][0]) % MOD;
printf("%lld\n",ans);
}
return 0;
}