Cod sursa(job #3183048)

Utilizator UengineDavid Enachescu Uengine Data 10 decembrie 2023 15:38:03
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.49 kb
#include <fstream>

using namespace std;

ifstream cin("kfib.in");
ofstream cout("kfib.out");

const int MOD = 666013;

class matrice{
public:
    long long mat[2][2];
    matrice(){
        this->mat[0][0] = 0;
        this->mat[0][1] = 1;
        this->mat[1][0] = 1;
        this->mat[1][1] = 1;
    }
    matrice(int x1, int x2){
        this->mat[0][0] = x1;
        this->mat[0][1] = x2;
        this->mat[1][0] = 0;
        this->mat[1][1] = 0;
    }
    matrice(int x00, int x01, int x10, int x11){
        this->mat[0][0] = x00;
        this->mat[0][1] = x01;
        this->mat[1][0] = x10;
        this->mat[1][1] = x11;
    }
    matrice operator *(const matrice &other)const{
        matrice ans;
        ans.mat[0][0] = (this->mat[0][0] * other.mat[0][0] + this->mat[0][1] * other.mat[1][0]) % MOD;
        ans.mat[0][1] = (this->mat[0][0] * other.mat[0][1] + this->mat[0][1] * other.mat[1][1]) % MOD;
        ans.mat[1][0] = (this->mat[1][0] * other.mat[0][0] + this->mat[1][1] * other.mat[1][0]) % MOD;
        ans.mat[1][1] = (this->mat[1][0] * other.mat[0][1] + this->mat[1][1] * other.mat[1][1]) % MOD;
        return ans;
    }
};

matrice lgput(matrice matr, int pow){
    matrice rez;
    while(pow){
        if(pow % 2 == 1)
            rez = (rez * matr);
        matr = (matr * matr);
        pow /= 2;
    }
    return rez;
}

matrice matrix, start(1, 1);

int main() {
    int x;
    cin >> x;
    matrix = lgput(matrix, x - 2);
    start = start * matrix;
    cout << start.mat[0][0];
    return 0;
}