Cod sursa(job #2862531)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 5 martie 2022 14:48:44
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.89 kb
#include "bits/stdc++.h"
 
using namespace std;
 
using ld = long double;
using ll = long long;
using ull = unsigned long long;
 
#if defined(ONPC)
#include "bits/debug.h"
#endif

const int MOD = 666013;

using Matrix = array<array<ll, 2>, 2>;

Matrix mul(Matrix a, Matrix b) {
    Matrix res = {{{0, 0}, {0, 0}}};
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 2; ++k) {
                res[i][j] += a[i][k] * b[k][j];
                res[i][j] %= MOD;
            }
        }
    }
    return res;
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    freopen("kfib.in", "r", stdin);
    freopen("kfib.out", "w", stdout);

    ll n;
    cin >> n;

    Matrix B = {{{1, 0}, {0, 1}}};
    Matrix F = {{{1, 1}, {1, 0}}};

    for (; n > 0; n /= 2, F = mul(F, F)) {
        if (n & 1) B = mul(B, F);
    }
    cout << B[0][1] << "\n";
}