洛谷 P1962 斐波那契数列

传送门

#include 
using namespace std;
#define ll long long 
#define MOD 1000000007
struct matrix {
	ll a1, a2, b1, b2;
	matrix(ll _a1, ll _a2, ll _b1, ll _b2) :a1(_a1), a2(_a2), b1(_b1), b2(_b2) {};
	matrix operator*(const matrix& y) {
		return  matrix((a1 * y.a1 + a2 * y.b1) % MOD,
			           (a1 * y.a2 + a2 * y.b2) % MOD, 
			           (b1 * y.a1 + b2 * y.b1) % MOD, 
			           (b1 * y.a2 + b2 * y.b2) % MOD);
	}
};
int main() {
	ll n;
	cin >> n;
	matrix ans=matrix(1, 0, 0, 1);
	matrix A = matrix(0, 1, 1, 1);
	n = n - 1;
	while (n) {
		if (n & 1)ans = ans * A;
		A = A * A;
		n = n >> 1;
	}
	cout << (ans.a1 + ans.a2) % MOD;
	return 0;
}

你可能感兴趣的:(洛谷 P1962 斐波那契数列)