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.
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
题目大意:已知F(1),F(2),F(N) = F(N-1) + 2F(N-2) + i^4,求F(N)
必备知识:
1、矩阵的乘法运算(自行百度)
2、将递推关系转化为矩阵 参考博客:将递推式转换成矩阵优化
3、矩阵快速幂,快速乘(计算过程中会超出long long范围,所以需要用到快速乘)
我们可以将 F(N) = F(N-1) + 2F(N-2) + i^4 转化为矩阵的形式:
[F(n) F(n-1) n^4 n^3 n^2 n^1 1] = [F(n-1) F(n-2) (n-1)^4 (n-1)^3 (n-1)^2 (n-1)^1 1] *
{{1,1,0,0,0,0,0}, ^ (n-1)
{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}};
上面的学会了代码就会写了。。我写的很乱。。
#include <stdio.h>
#define mod 2147493647ll
void calc(long long a[7][7],long long b[7][7]);
long long matrix[7][7] = {{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}};
long long mat_temp[7][7];
long long pow_mult(long long a,long long b)
{
long long ans = 0;
while (b > 0)
{
if (b & 1) ans = (ans + a) % mod;
a = (a + a) % (unsigned long long)mod;
b = b / 2;
}
return ans;
}
void pow_mod(long long a[7][7],long long n)
{
long long ans[7][7];
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
ans[i][j] = matrix[i][j];
while (n > 0)
{
if (n & 1) calc(ans,a);
calc(a,a);
n = n / 2;
}
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
a[i][j] = ans[i][j];
}
void calc(long long a[7][7],long long b[7][7])
{
long long temp[7][7];
long long num = 0;
for (int i = 0; i < 7; i++)
{
for (int j = 0; j < 7; j++)
{
num = 0;
for (int k = 0; k < 7; k++)
{
num = num + pow_mult(a[i][k],b[k][j]);
}
temp[i][j] = num;
}
}
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
a[i][j] = temp[i][j];
}
int main()
{
int t;
long long a,b,n;
scanf("%d",&t);
while (t--)
{
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7; j++)
mat_temp[i][j] = matrix[i][j];
scanf("%lld%lld%lld",&n,&a,&b);
if (n == 1)
{
printf("%lld\n",a);
continue;
}
if (n == 2)
{
printf("%lld\n",b);
continue;
}
pow_mod(mat_temp,n-3);
long long ans = 0;
long long num[7] = {0,0,16,8,4,2,1};
num[0] = b;
num[1] = a;
for (int i = 0; i < 7; i++)
{
ans = (ans + pow_mult(num[i],mat_temp[i][0])) % mod;
//printf("%lld\n",ans);
}
printf("%lld\n",ans);
}
return 0;
}