0 1 0 6 10 2
0 60
#include<stdio.h> #define mod 1000000007 typedef __int64 LL; struct Matrix { LL mat[2][2]; }; Matrix unit_matrix = { 1, 0, 0, 1 }; //单位矩阵 Matrix mul(Matrix a, Matrix b) //矩阵相乘 { Matrix res; for(int i = 0; i < 2; i++) for(int j = 0; j < 2; j++) { res.mat[i][j] = 0; for(int k = 0; k < 2; k++) { res.mat[i][j] += a.mat[i][k] * b.mat[k][j]; res.mat[i][j] %= 1000000006; } } return res; } Matrix pow_matrix(Matrix a, LL n) //矩阵快速幂 { Matrix res = unit_matrix; while(n != 0) { if(n & 1) res = mul(res, a); a = mul(a, a); n >>= 1; } return res; } LL pow(LL a, LL n) //二分快速幂 { a %= mod; LL res = 1; while(n != 0) { if(n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { LL a, b, n; Matrix tmp; tmp.mat[0][0] = 0; tmp.mat[0][1] = tmp.mat[1][0] = tmp.mat[1][1] = 1; while(~scanf("%I64d%I64d%I64d",&a,&b,&n)) { Matrix p = pow_matrix(tmp, n); //p.mat[0][0]即为F(n-1),p.mat[1][0]即为F(n) LL ans = (pow(a, p.mat[0][0]) * pow(b, p.mat[1][0])) % mod; printf("%I64d\n",ans); } return 0; }
hdu 4549 M斐波那契数列(费马小定理 + 二分快速幂 + 矩阵快速幂),布布扣,bubuko.com
hdu 4549 M斐波那契数列(费马小定理 + 二分快速幂 + 矩阵快速幂)
原文:http://blog.csdn.net/lyhvoyage/article/details/22896257