80 pts,求助!
查看原帖
80 pts,求助!
814343
bc2_cryeggy楼主2023/10/25 20:47

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>

using namespace std;

using ll = long long;

const int kMaxN = 110, kInf = (((1 << 30) - 1) << 1) + 1, kMod = 1e9 + 7;

struct matrix {
	ll mtx[kMaxN][kMaxN];
  matrix operator *(const matrix &b) const {
    matrix c;
    for (int i = 1; i <= 2; ++ i) {
      for (int j = 1; j <= 2; ++ j) {
        c.mtx[i][j] = 0;
        for (int k = 1; k <= 2; ++ k) {
          c.mtx[i][j] = (c.mtx[i][j] + mtx[i][k] * b.mtx[k][j] % kMod) % kMod;
        }
      }
    }
    return c;
  }
  matrix operator ^(int k) {
    matrix ans, x = *this;
    memset(ans.mtx, 0, sizeof ans.mtx);
    for (int i = 1; i <= 2; ++ i) {
      ans.mtx[i][i] = 1;
    }
    for (; k; k >>= 1, x = x * x) {
      if (k & 1) {
        ans = ans * x;
      }
    }
    return ans;
  }
} f;

ll n, q;

int main() {
//  freopen(".in", "r", stdin);
//  freopen(".out", "w", stdout);
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> q;
  f.mtx[1][1] = 1, f.mtx[1][2] = 1;
  f.mtx[2][1] = 1, f.mtx[2][2] = 0;
  f = f ^ q;
  cout << f.mtx[2][1] % kMod << '\n';
  return 0;
}
2023/10/25 20:47
加载中...