Recursive sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2774 Accepted Submission(s): 1245
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
23 1 24 1 10
Sample Output
85369
Hint
In 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.
很容易看出是一个矩阵快速幂的题
1 2 1 0 0 0 0 F(i-1) F(i)
1 0 0 0 0 0 0 F(i-2) F(i-1)
0 0 1 4 6 4 1 i^4 (i+1)^4
0 0 0 1 3 3 1 * i^3 = (i+1)^3
0 0 0 0 1 2 1 i^2 (i+1)^2
0 0 0 0 0 1 1 i^1 (i+1)^1
0 0 0 0 0 0 1 1 1
所以
1 2 1 0 0 0 0 F(2) F(i)
1 0 0 0 0 0 0 F(1) F(i-1)
0 0 1 4 6 4 1 3^4 (i+1)^4
0 0 0 1 3 3 1 ^(n-2) * 3^3 = (i+1)^3
0 0 0 0 1 2 1 3^2 (i+1)^2
0 0 0 0 0 1 1 3^1 (i+1)^1
0 0 0 0 0 0 1 1 1
这里回忆一个二项式定理:
(a+b)^n=C(n,0)a^n+C(n,1)a^(n-1)b+...+C(n,r)a^(n-r)b^r+...+C(n,n)b^n
其中 C(n,m) = n*(n-1)*...*(n-m)/m!; 即C(4,2) = 4*3/(2*1) = 6;
ac代码
#include<bits/stdc++.h>
#define MOD 2147493647
using namespace std;
typedef long long LL;
LL n,m;
//矩阵结构体
struct Mat
{
LL mat[15][15];
};
//乘法模板
Mat operator*(Mat a,Mat b)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for(int k = 0; k < 7;k++)
for(int i = 0; i < 7; i++)
for(int j = 0; j < 7; j++)
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k]*b.mat[k][j])%MOD;
return c;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
Mat sum,temp;
memset(sum.mat,0,sizeof(sum.mat));
memset(temp.mat,0,sizeof(temp.mat));
scanf("%lld",&n);
//构建原矩阵
LL t1,t2;
scanf("%lld %lld",&t1,&t2);
sum.mat[0][0] = t2;
sum.mat[1][0] = t1;
sum.mat[2][0] = 81;
sum.mat[3][0] = 27;
sum.mat[4][0] = 9;
sum.mat[5][0] = 3;
sum.mat[6][0] = 1;
//构建快速幂矩阵
temp.mat[0][0] = temp.mat[0][2] = 1; temp.mat[0][1] = 2;
temp.mat[1][0] = 1;
temp.mat[2][2] = temp.mat[2][6] = 1;temp.mat[2][3] = temp.mat[2][5] = 4;temp.mat[2][4] = 6;
temp.mat[3][3] = temp.mat[3][6] = 1;temp.mat[3][4] = temp.mat[3][5] = 3;
temp.mat[4][4] = temp.mat[4][6] = 1;temp.mat[4][5] =2;
temp.mat[5][5] = temp.mat[5][6] = 1;
temp.mat[6][6] = 1;
Mat res;
memset(res.mat,0,sizeof(res.mat));
for(int i = 0; i < 7; i++)
res.mat[i][i] = 1;
//m次方
m = n-2;
while(m>0)
{
if(m&1)
res = res*temp;
temp = temp*temp;
m/=2;
}
sum = res*sum;//特别注意这里 是和res相乘
printf("%lld\n",sum.mat[0][0]);
}
return 0;
}