标签:des style blog http color io os ar for
The input test file will contain a single line containing n (n ≤ 2^31-1).
There are multiple test cases!
For each test case, print the Fn mod (10^9 + 7).
9
34
用矩阵快速幂的方法,具体可见: http://blog.csdn.net/ACdreamers/article/details/25616461
#include <iostream> using namespace std; #define M 1000000007 struct Matrix{ long long v[2][2]; }; Matrix matrixMul(Matrix a, Matrix b) { Matrix temp; for (int i = 0; i != 2; i++) { for (int j = 0; j != 2; j++) { temp.v[i][j] = 0; for (int k = 0; k != 2; k++) { temp.v[i][j] += a.v[i][k] * b.v[k][j]; temp.v[i][j] %= M; } } } return temp; } Matrix power(Matrix a, Matrix b, long long n) { while (n) { if (n & 1) { b = matrixMul(b, a); } n >>= 1; a = matrixMul(a, a); } return b; } int main(int argc, char* argv[]) { Matrix a = {1, 1, 1, 0}, b = {1, 0, 0, 1}; long long n; while (cin >> n) { if (n == 0) cout << 0 << endl; else { Matrix result = power(a, b, n - 1); cout << result.v[0][0] << endl; } } return 0; }
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/xiezhw3/p/3997190.html