Recursive sequence(矩阵快速幂)
2019-08-02 19:43 木木王韦 阅读(194) 评论(0) 收藏 举报Recursive sequence
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.
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
| 1 2 1 0 0 0 0 | * | F(n-1) 0 0 0 0 0 0 | = | F(n)___ 0 0 0 0 0 0 |
| 1 0 0 0 0 0 0 | * | F(n-2) 0 0 0 0 0 0 | = | F(n-1)_ 0 0 0 0 0 0 |
| 0 0 1 4 6 4 1 | * | n^4__ 0 0 0 0 0 0 | = | (n+1)^4 0 0 0 0 0 0 |
| 0 0 0 1 3 3 1 | * | n^3__ 0 0 0 0 0 0 | = | (n+1)^3 0 0 0 0 0 0 |
| 0 0 0 0 1 2 1 | * | n^2__ 0 0 0 0 0 0 | = | (n+1)^2 0 0 0 0 0 0 |
| 0 0 0 0 0 1 1 | * | n____ 0 0 0 0 0 0 | = | (n+1)__ 0 0 0 0 0 0 |
| 0 0 0 0 0 0 1 | * | 1____ 0 0 0 0 0 0 | = | 1_____ 0 0 0 0 0 0 |
当n>=3时符合上述矩阵公式
即ac代码:
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct node{
long long int m[10][10];
};
int f=7;
long long int mod=2147493647;
node a,b,c;
int m,n,k;
node mul(node a,node b){
node ans;
memset(ans.m ,0,sizeof(ans.m ));
for(int i=1;i<=f;i++){
for(int j=1;j<=f;j++){
for(int k=1;k<=f;k++){
ans.m [i][j]=(ans.m [i][j]+a.m [i][k]*b.m [k][j]%mod)%mod;
}
}
}
return ans;
}
node ksm(node a,int b){
node res;
memset(res.m,0,sizeof(res.m));
for(int i=1;i<=f;i++){
res.m[i][i]=1;
}
while(b){
if(b&1){
res=mul(res,a);
}
b>>=1;
a=mul(a,a);
}
/*for(int i=1;i<=f;i++){
for(int j=1;j<=f;j++){
cout<<a.m[i][j]<<" ";
}
cout<<endl;
}*/
return res;
}
int main(){
int t;
cin>>t;
while(t--){
cin>>m>>n>>k;
if(m==1){
cout<<n%mod<<endl;
}
else if(m==2){
cout<<k%mod<<endl;
}
else{
memset(a.m ,0,sizeof(a.m ));
a.m[1][1]=1;a.m[1][2]=2;a.m[1][3]=1;
a.m[2][1]=1;
a.m[3][3]=1;a.m[3][4]=4;a.m[3][5]=6;a.m[3][6]=4;a.m[3][7]=1;
a.m[4][4]=1;a.m[4][5]=3;a.m[4][6]=3;a.m[4][7]=1;
a.m[5][5]=1;a.m[5][6]=2;a.m[5][7]=1;
a.m[6][6]=1;a.m[6][7]=1;
a.m[7][7]=1;
/* for(int i=1;i<=f;i++){
for(int j=1;j<=f;j++){
cout<<a.m[i][j]<<" ";
}
cout<<endl;
}*/
memset(b.m,0,sizeof(b.m));
b.m[1][1]=k%mod;
b.m[2][1]=n%mod;
b.m[3][1]=81;
b.m[4][1]=27;
b.m[5][1]=9;
b.m[6][1]=3;
b.m[7][1]=1;
c=ksm(a,m-2);
/* for(int i=1;i<=f;i++){
for(int j=1;j<=f;j++){
cout<<c.m[i][j]<<" ";
}
cout<<endl;
}*/
c=mul(c,b);
cout<<c.m[1][1]%mod<<endl;
}
}
return 0;
}
浙公网安备 33010602011771号