ivnikkk

Untitled

Dec 19th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.92 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS  
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(a) a.begin(), a.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef long double ld;
  9. const ll mod = 1e9 + 7;
  10. struct Matrix {
  11.     ll n, m;
  12.     vector<vector<ll>> a;
  13.     Matrix(ll _n, ll _m) {
  14.         n = _n, m = _m;
  15.         a.resize(n, vector<ll>(m,0));
  16.     }
  17.     Matrix(ll _n, ll _m, vector<vector<ll>> &b) {
  18.         n = _n, m = _m;
  19.         a.resize(n, vector<ll>(m,0));
  20.         a = b;
  21.     }
  22.     void assign_ed() {
  23.         for (ll i = 0; i < min(n, m); i++)a[i][i] = 1;
  24.     }
  25.     Matrix operator*(const Matrix& other) const {
  26.         vector<vector<ll>> c(other.n, vector<ll>(other.m));
  27.         for (ll k = 0; k < m; k++) {
  28.             for (ll i = 0; i < other.n; i++) {
  29.                 for (ll j = 0; j < other.m; j++) {
  30.                     c[i][j] += (a[i][k] * other.a[k][j])%mod;
  31.                     c[i][j] %= mod;
  32.                 }
  33.             }
  34.         }
  35.         return Matrix(other.n, other.m, c);
  36.     }
  37. };
  38. Matrix Fastpow(Matrix a, ll n) {
  39.     Matrix res(a.n, a.m);
  40.     res.assign_ed();
  41.     while (n) {
  42.         if (n & 1) {
  43.             res = res * a;
  44.         }
  45.         a = a * a;
  46.         n >>= 1;
  47.     }
  48.     return res;
  49. }
  50. signed main() {
  51. #ifdef _DEBUG
  52.     freopen("input.txt", "r", stdin);
  53.     freopen("output.txt", "w", stdout);
  54. #endif
  55.     ios_base::sync_with_stdio(false);
  56.     cin.tie(nullptr);
  57.     cout.tie(nullptr);
  58.     Matrix Vec_res(3, 1), Base(3, 3);
  59.     for (int i = 0; i < 3; i++)Base.a[0][i] = 1;
  60.     Base.a[1][0] = 1;
  61.     Base.a[2][1] = 1;
  62.     Vec_res.a[0][0] = 7, Vec_res.a[1][0] = 4, Vec_res.a[2][0] = 2;
  63.     ll n;
  64.     cin >> n;
  65.     n--;
  66.     if (n <= 2) {
  67.         cout << Vec_res.a[2 - n][0];
  68.     }
  69.     else {
  70.         n -= 2;
  71.         Base = Fastpow(Base, n);
  72.         Vec_res = Base * Vec_res;
  73.         cout << Vec_res.a[0][0];
  74.     }
  75. }
Add Comment
Please, Sign In to add comment