首先构造矩阵
A1 A2
0 1 0 0 f0 f1
0 0 1 0 * f1 = f2
7 2 3 0 f2 f3
0 0 1 1 f0+f1 f0+f1+f2
然后首先求前面的矩阵A1的m-1次方然后乘以A2
即可得到答案
注意矩阵的大小。。。。TLE了半天
AC代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAX_N = 6;
const int MAX_M = 6;
const int MOD = 2009;
struct Matrix{
int M, N;
int num[MAX_M][MAX_N];
void init(){
memset( num, 0, sizeof( num ) );
}
};
Matrix multiply( Matrix a, Matrix b ){
Matrix c;
c.M = a.M;
c.N = b.N;
c.init();
for( int i = 1; i <= c.M; i++ ){
for( int j = 1; j <= c.N; j++ ){
for( int k = 1; k <= a.N; k++ ){
c.num[i][j] = ( c.num[i][j] + a.num[i][k] * b.num[k][j] ) % MOD;
}
}
}
return c;
}
Matrix get_mul_pow( Matrix a, long long n ){
Matrix ans;
ans = a;
ans.init();
for( int i = 1; i <= a.M; i++ ){
ans.num[i][i] = 1;
}
while( n ){
if( n % 2 == 1 ){
ans = multiply( ans, a );
}
a = multiply( a, a );
n /= 2;
}
return ans;
}
int main(){
Matrix f;
f.M = f.N = 4;
f.num[1][1] = 0;f.num[1][2] = 1;f.num[1][3] = 0;f.num[1][4] = 0;
f.num[2][1] = 0;f.num[2][2] = 0;f.num[2][3] = 1;f.num[2][4] = 0;
f.num[3][1] = 7;f.num[3][2] = 2;f.num[3][3] = 3;f.num[3][4] = 0;
f.num[4][1] = 0;f.num[4][2] = 0;f.num[4][3] = 1;f.num[4][4] = 1;
int T, Case = 1;
cin >> T;
while( T-- ){
long long n;
scanf( "%I64d", &n );
printf( "Case %d: ", Case++ );
// cout << "Case " << Case++ << ": ";
if( n == 0 ){
printf( "%d\n", 1 );
}else if( n == 1 ){
printf( "%d\n", 4 );
}else{
Matrix t = get_mul_pow( f, n - 1 );
Matrix tt;
tt.M = 4;
tt.N = 1;
tt.num[1][1] = 1;
tt.num[2][1] = 3;
tt.num[3][1] = 5;
tt.num[4][1] = 4;
Matrix ans = multiply( t, tt );
printf( "%d\n", ans.num[4][1] );
}
}
return 0;
}