Advertisement
ivnikkk

Untitled

May 7th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 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. struct Matrix {
  10.     ll n, m;
  11.     vector<vector<ll>> a;
  12.     Matrix(ll _n, ll _m) {
  13.         n = _n, m = _m;
  14.         a.resize(n, vector<ll>(m,0));
  15.     }
  16.     Matrix(ll _n, ll _m, vector<vector<ll>> &b) {
  17.         n = _n, m = _m;
  18.         a.resize(n, vector<ll>(m,0));
  19.         a = b;
  20.     }
  21.     void assign_ed() {
  22.         for (ll i = 0; i < min(n, m); i++)a[i][i] = 1;
  23.     }
  24.     Matrix operator*(const Matrix& other) const {
  25.         vector<vector<ll>> c(other.n, vector<ll>(other.m));
  26.         for (ll k = 0; k < m; k++) {
  27.             for (ll i = 0; i < other.n; i++) {
  28.                 for (ll j = 0; j < other.m; j++) {
  29.                     c[i][j] += a[i][k] * other.a[k][j];
  30.                 }
  31.             }
  32.         }
  33.         return Matrix(other.n, other.m, c);
  34.     }
  35. };
  36. Matrix Fastpow(Matrix a, ll n) {
  37.     Matrix res(a.n, a.m);
  38.     res.assign_ed();
  39.     while (n) {
  40.         if (n & 1) {
  41.             res = res * a;
  42.         }
  43.         a = a * a;
  44.         n >>= 1;
  45.     }
  46.     return res;
  47. }
  48. signed main() {
  49. #ifdef _DEBUG
  50.     freopen("input.txt", "r", stdin);
  51.     freopen("output.txt", "w", stdout);
  52. #endif
  53.     ios_base::sync_with_stdio(false);
  54.     cin.tie(nullptr);
  55.     cout.tie(nullptr);
  56.     Matrix Fib(2, 2),Vec_res(2,1);
  57.     Fib.a[0][0] = 1;
  58.     Fib.a[0][1] = 1;
  59.     Fib.a[1][0] = 1;
  60.     Fib.a[1][1] = 0;
  61.    
  62.     Vec_res.a[0][0] = 1;
  63.     Vec_res.a[1][0] = 1;
  64.     ll n;
  65.     cin >> n;
  66.     n--;
  67.     Fib = Fastpow(Fib, n);
  68.     Vec_res = Fib * Vec_res;
  69.     cout << Vec_res.a[1][0];
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement