题目链接:传送门
题目:
Recursive sequence
Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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-)-th number, the (i-)-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 < 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 . Sample Input Sample Output Hint
In the first case, the third number is = *1十2十3^.
In the second case, the third number is = *1十1*10十3^ and the fourth number is = * 十 十 ^.
题目大意:
已知a1 = a,a2 = b,ai = ai-1 + 2ai-2 + n4,求aN,答案对2147493647取模。
N,a,b < 231。
思路:
因为N超级大,考虑快速幂,但是通项怎么都搞不出来。
又题目给的是递推式,考虑用矩阵快速幂。。。。
令Fn = [an-1, an],则Fn-1 = [an-2,an-1],但是这样an+1就算不上n4了。那就把n4加上去试试看:
再令Fn = [an-1,an,(n+1)4],这样就可以推出an+1了,但是(n+1)4又不能递推。。。展开(n+1)4发现:(n+1)4 = n4 + 4n3 + 6n2 + 4n + 1,可以由n4、n3、n2、n、1递推出来。同时(n+1)3、(n+1)2、n+1、1都可以用n4、n3、n2、n、1递推出来,所以Fn和系数矩阵base就出来了:
Fn = [an-1,an,(n+1)4,(n+1)3,(n+1)2,n+1,1];
$\begin{bmatrix}
0 &2 &0 &0 &0 &0 &0 \\
1 &1 &0 &0 &0 &0 &0 \\
0 &1 &1 &0 &0 &0 &0 \\
0 &0 &4 &1 &0 &0 &0 \\
0 &0 &6 &3 &1 &0 &0 \\
0 &0 &4 &3 &2 &1 &0 \\
0 &0 &1 &1 &1 &1 &1
\end{bmatrix}$
代码:
#include <bits/stdc++.h> using namespace std;
typedef long long ll;
#define MAXN 7
const ll mod = ; struct Matrix
{
ll mat[MAXN][MAXN];
Matrix() {}
Matrix operator*(Matrix const &b)const
{
Matrix res;
memset(res.mat, , sizeof(res.mat));
for (int i = ;i < MAXN; i++)
for (int j = ; j < MAXN; j++)
for (int k = ; k < MAXN; k++)
res.mat[i][j] = (res.mat[i][j]+this->mat[i][k] * b.mat[k][j])%mod;
return res;
}
}base;
Matrix pow_mod(Matrix base, ll n)
{
Matrix res;
memset(res.mat, , sizeof(res.mat));
for (int i = ; i < MAXN; i++)
res.mat[i][i] = ;
while (n > )
{
if (n & ) res = res*base;
base = base*base;
n >>= ;
}
return res;
}//输入基础矩阵返回n次幂的矩阵;
//res.mat[0][0] 就是最终的值F(N+1)
//注意修改MAXN和mod void init() {
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ; base.mat[][] = ;
} int main()
{
init();
int T;
cin >> T;
while (T--) {
ll n, a, b;
scanf("%lld%lld%lld", &n, &a, &b);
Matrix F2;
memset(F2.mat, , sizeof F2.mat);
F2.mat[][] = a; F2.mat[][] = b; F2.mat[][] = ***; F2.mat[][] = **; F2.mat[][] = *; F2.mat[][] = ; F2.mat[][] = ;
Matrix FN = F2*pow_mod(base, n-);
cout << FN.mat[][] << endl;
}
return ;
}