版本比较
题解 P1962 【斐波那契数列】
正文
增加 13 行删除 13 行
54 行未改动555657585959606161626363646565666768686970717272737475767778767778798081828381828384858686878889909154 行未改动# 二、矩阵乘法解法不管上面的邪门做法了,矩阵乘法才是正解。qwq```cpp#include <bits/stdc++.h>#define ll long long #define ll long longusing namespace std;const ll P = 1000000007; const ll P = 1000000007;struct mat { ll a[3][3]; ll a[3][3]; mat operator * (const mat& b) const { mat res; mat res; for (int i = 1; i <= 2; i ++) { for (int j = 1; j <= 2; j ++) { res.a[i][j] = 0; res.a[i][j] = 0; for (int k = 1; k <= 2; k ++) res.a[i][j] = ((res.a[i][j] + a[i][k] * b.a[k][j] % P) % P + P) % P; } } return res; return res; }} Base;mat qpow(mat x, ll y) { mat res = x; y -= 1; for (; y; y >>= 1, x = x * x) if (y & 1) res = res * x; return res; mat res = x; y -= 1; for (; y; y >>= 1, x = x * x) if (y & 1) res = res * x; return res;}int main() { Base.a[1][1] = 1; Base.a[1][2] = 1; Base.a[2][1] = 1; Base.a[2][2] = 0; ll x; scanf("%lld", &x); Base.a[1][1] = 1; Base.a[1][2] = 1; Base.a[2][1] = 1; Base.a[2][2] = 0; ll x; scanf("%lld", &x); if (x <= 2) printf("1\n"); else { mat res = qpow(Base, x); mat res = qpow(Base, x); printf("%lld\n", res.a[1][2]); } return 0;}```