正文
增加 13 行删除 13 行
54 行未改动5556575859606162636465666768697071727374757677787980818283848586878889909154 行未改动# 二、矩阵乘法解法不管上面的邪门做法了,矩阵乘法才是正解。qwq```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;const ll P = 1000000007;struct mat {ll a[3][3];mat operator * (const mat& b) const {mat res;for (int i = 1; i <= 2; i ++) {for (int j = 1; j <= 2; j ++) {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;}} 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;}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);if (x <= 2) printf("1\n");else {mat res = qpow(Base, x);printf("%lld\n", res.a[1][2]);}return 0;}```54 行未改动5556575859606162636465666768697071727374757677787980818283848586878889909154 行未改动# 二、矩阵乘法解法不管上面的邪门做法了,矩阵乘法才是正解。qwq```cpp#include <bits/stdc++.h>#define ll long longusing namespace std;const ll P = 1000000007;struct mat {ll a[3][3];mat operator * (const mat& b) const {mat res;for (int i = 1; i <= 2; i ++) {for (int j = 1; j <= 2; j ++) {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;}} 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;}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);if (x <= 2) printf("1\n");else {mat res = qpow(Base, x);printf("%lld\n", res.a[1][2]);}return 0;}```