Cod sursa(job #3203318)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 13 februarie 2024 15:03:57
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
const char sp = ' ';
const int inf = 0x3f3f3f3f;
const int mod = 666013;
const char out[2][4]{ "NO", "YES" };
#define all(a) a.begin(), a.end()
using ll = long long;
ifstream fin("kfib.in");
ofstream fout("kfib.out");

using matrix = vector<vector<ll>>;

matrix fib{
    { 1, 1 },
    { 1, 0 }
};

matrix id{
    { 1, 0 },
    { 0, 1 }
};

matrix multiply(const matrix& a, const matrix& b) {
    matrix c(a.size(), vector<ll>(b[0].size()));
    for (int i = 0; i < a.size(); ++i) {
        for (int j = 0; j < b[0].size(); ++j) {
            for (int k = 0; k < a[0].size(); ++k) {
                c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
            }
        }
    }
    return c;
}

matrix ppow(matrix n, ll p) {
    if (p == 0) {
        return id;
    }
    matrix rt = ppow(n, p >> 1);
    return multiply(multiply(rt, rt), p & 1 ? n : id);
}

ll n;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    fin >> n;
    fout << ppow(fib, n - 1)[0][0];
}