Cod sursa(job #3298470)

Utilizator Bolbotina_David-AndreiBolbotina David-Andrei Bolbotina_David-Andrei Data 30 mai 2025 13:03:36
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.42 kb
/******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("kfib.in");
ofstream fout("kfib.out");

void inmultire(int Z_prev[2][2], int Z1[2][2], int Z_curr[2][2]){
    int temp[2][2];
    temp[0][0] = (1LL * Z_prev[0][0] * Z1[0][0] + 1LL * Z_prev[0][1] * Z1[1][0]) % 666013;
    temp[0][1] = (1LL * Z_prev[0][0] * Z1[0][1] + 1LL * Z_prev[0][1] * Z1[1][1]) % 666013;
    temp[1][0] = (1LL * Z_prev[1][0] * Z1[0][0] + 1LL * Z_prev[1][1] * Z1[1][0]) % 666013;
    temp[1][1] = (1LL * Z_prev[1][0] * Z1[0][1] + 1LL * Z_prev[1][1] * Z1[1][1]) % 666013;
    
    Z_curr[0][0] = temp[0][0];
    Z_curr[0][1] = temp[0][1];
    Z_curr[1][0] = temp[1][0];
    Z_curr[1][1] = temp[1][1];
}

void exp_log(int x[2][2], int n, int Z[2][2]) {
    while (n > 0) {
        if (n % 2)
            inmultire(Z,x,Z);
        inmultire(x,x,x);
        n /= 2;
    }
}

int main() {
    
    int k;
    fin>>k;
    
    int Z1[2][2] = {{0, 1},{1, 1}};
    int rez[2][2] = {{1,0},{0,1}};
    exp_log(Z1, k-1, rez);
    fout<<rez[1][1]<<endl;

	return 0;
}