标签:
题目大意:A(0) = 1 , A(1) = 1 , A(N) = X * A(N - 1) + Y * A(N - 2) (N >= 2).
And we want to Calculate S(N) , S(N) = A(0) 2 +A(1) 2+……+A(n) 2.
解题思路:将An^2化开,得x * x * A(n-1) * A(n-1) + y * y * A(n-2) * A(n-2) + 2 * x * y * A(n-1) * A(n-2),由这个公式就可以得到矩阵了
#include<cstdio>
typedef long long ll;
const int N = 4;
const ll mod = 10007;
struct Matrix{
ll mat[N][N];
}A, B, tmp;
ll n, X, Y;
void init() {
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++) {
A.mat[i][j] = B.mat[i][j] = 0;
if(i == j)
B.mat[i][j] = 1;
}
A.mat[0][0] = A.mat[1][2] = 1;
A.mat[1][0] = A.mat[1][1] = (X * X) % mod;
A.mat[2][0] = A.mat[2][1] = (Y * Y) % mod;
A.mat[3][0] = A.mat[3][1] = (2 * X * Y) % mod;
A.mat[1][3] = X % mod;
A.mat[3][3] = Y % mod;
}
Matrix matMul(Matrix x, Matrix y) {
for(int i = 0; i < N; i++)
for(int j = 0; j < N; j++) {
tmp.mat[i][j] = 0;
for(int k = 0; k < N; k++)
tmp.mat[i][j] += (x.mat[i][k] * y.mat[k][j] ) % mod;
}
return tmp;
}
void solve() {
while(n) {
if(n & 1)
B = matMul(B,A);
A = matMul(A,A);
n >>= 1;
}
}
int main() {
while(scanf("%I64d%I64d%I64d", &n, &X, &Y) != EOF) {
if(n == 0) {
printf("1\n");
continue;
}
else if(n == 1) {
printf("2\n");
continue;
}
init();
n--;
solve();
printf("%I64d\n", (2 * B.mat[0][0] + B.mat[1][0] + B.mat[2][0] + B.mat[3][0] ) % mod);
}
return 0;
}
HDU - 3306 Another kind of Fibonacci 矩阵快速幂
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/46058717