Cod sursa(job #3002752)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 15 martie 2023 09:02:09
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include <fstream>
#include <vector>

#define MOD 666013
#define VLL vector<vector<long long>>

using namespace std;

ifstream f("kfib.in");
ofstream g("kfib.out");

int n;

void product(VLL a, VLL b, VLL &sol) {
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            sol[i][j] = 0;
            for (int k = 0; k < 2; k++) {
                sol[i][j] += (a[i][k] * b[k][j]) % MOD;
                sol[i][j] %= MOD;
            }
        }
    }
}

int main() {
    f >> n;

    VLL sol = {{1, 0},
               {0, 1}},
            a = {{1, 1},
                 {1, 0}},
            b = {{0, 0},
                 {0, 0}};

    n -= 2;
    while (n) {
        if (n % 2 == 1) {
            product(a, sol, b);
            sol = b;
            n--;
        }
        product(a, a, b);
        a = b;
        n /= 2;
    }

    g << (sol[0][0] + sol[0][1]) % MOD;
    return 0;
}