Cod sursa(job #2975226)

Utilizator Paul281881818818181991919191881818Draghici Paul Paul281881818818181991919191881818 Data 5 februarie 2023 21:39:02
Problema Al k-lea termen Fibonacci Scor 5
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){
    if(n == 1)
        return A;
    if(n % 2){
        return A*power(A, n-1);
    }
    else
        return power(A, n/2) * power(A, n/2); 
}
int main(){
    int k;
    fin >> k;
    matrix mat;
    matrix res_mat = power(mat, k-1);
    fout << (res_mat.container[0][0] + res_mat.container[0][1]);
}