Cod sursa(job #3331997)

Utilizator Radu_BicliBiclineru Radu Radu_Bicli Data 2 ianuarie 2026 19:28:46
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.07 kb
#include <bits/stdc++.h>

using namespace std;

#define USE_STD_IO 0
#if USE_STD_IO
    #define fin cin
    #define fout cout
#else
    ifstream fin("kfib.in");
    ofstream fout("kfib.out");
#endif // USE_STD_IO

typedef long long Matrice[2][2];
const long long MOD = 666013;
Matrice a, p;
long long n;

static inline void Inmul(Matrice a, Matrice b) {
    Matrice c;
    c[0][0] = c[0][1] = 0;
    c[1][0] = c[1][1] = 0;
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            for(int k = 0; k < 2; k++) {
                c[i][j] = (c[i][j] + (a[i][k] * b[k][j]) % MOD) % MOD;
            }
        }
    }
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) a[i][j] = c[i][j];
    }
}

int main() {
    if(USE_STD_IO) ios_base::sync_with_stdio(false);
    fin.tie(NULL);
    fout.tie(NULL);

    fin >> n;

    a[0][1] = a[1][0] = a[1][1] = 1;
    p[0][0] = p[1][1] = 1;
    while(n) {
        if(n & 1) Inmul(p, a);
        Inmul(a, a);
        n >>= 1;
    }
    fout << p[0][1];

    return 0;
}