Cod sursa(job #2975246)

Utilizator Paul281881818818181991919191881818Draghici Paul Paul281881818818181991919191881818 Data 5 februarie 2023 21:47:56
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>
#define MOD 666013
std::ifstream fin("kfib.in");
std::ofstream fout("kfib.out");
class matrix{
    public:
    long long int container[2][2];
    matrix(){
        container[0][0] = 0,
        container[0][1] = 1,
        container[1][0] = 1,
        container[1][1] = 1;
    }
    matrix operator* (const matrix& obj1){
        matrix res;
        for(int i=0; i<2; i++){
            for(int j=0; j<2; j++){
                res.container[i][j] = 0;
                for(int z=0; z<2; z++){
                    res.container[i][j] += (container[i][z] * obj1.container[z][j]) % MOD;
                    res.container[i][j] %= MOD;
                }
            }
        }
        return res;
    }
    friend std::ostream& operator <<(std::ostream& fout, const matrix& obj){
        for(int i=0; i<2; i++){
            for(int j=0; j<2; j++){
                fout << obj.container[i][j] << " ";
            }
            fout << "\n";
        }
        return fout;
    }
};
matrix power(matrix& A , int n){
    matrix res = A;
    while(n){
        if(n%2 == 1){
            res = res * A;
        }
        A = A * A;
        n /= 2;
    }
    return res;
}
int main(){
    long long int k;
    fin >> k;
    matrix mat;
    matrix res_mat = power(mat, k-1);
    fout << res_mat.container[1][0];
}