Cod sursa(job #2932230)

Utilizator AlexandruBenescuAlexandru Benescu AlexandruBenescu Data 2 noiembrie 2022 11:40:44
Problema A+B Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>
using namespace std;

template <class type>
using matrix = vector <vector <type>>;

using ll = long long;

struct Matrix{
  int size_;
  matrix <ll> content_;

  Matrix (int size_of_matrix, int value = 0){
    size_ = size_of_matrix;
    content_.resize(size_, vector <ll> (size_));

    for (int i = 0; i < size_; i++)
      content_[i][i] = value;
  }

  vector <ll>& operator [] (const int i){
    return content_[i];
  }

  Matrix operator * (const Matrix& rhs) const{
    Matrix result (this -> size_);
    for (int i = 0; i < this -> size_; i++)
      for (int j = 0; j < this -> size_; j++)
        for (int k = 0; k < this -> size_; k++)
          result[i][j] += (*this)[i][k] * rhs[k][j];
    return result;
  }

  Matrix& operator *= (const Matrix& rhs){
    (*this) = (*this) * rhs;
    return (*this);
  }

  Matrix operator ^ (int p) const{
    Matrix result(this -> size_, 1);
    Matrix aux = (*this);
    for (; p; p >>= 1){
      if (p & 1)
        result *= aux;
      aux *= aux;
    }
    return result;
  }
};

int main(){
  long long n; cin >> n;

  Matrix x(2);
  x[0][0] = 5;
  x[1][0] = 1;

  if (n < 3){
    cout << x[2 - n][0] << "\n";
    return 0;
  }

  Matrix a(2);
  a[0][0] = 5; a[0][1] = -2;
  a[1][0] = 1; a[1][1] = 0;
  a = a ^ (n - 2);

  Matrix y = a * x;

  cout << y[0][0] << "\n";

  return 0;
}