Cod sursa(job #2853879)

Utilizator Edyci123Bicu Codrut Eduard Edyci123 Data 20 februarie 2022 18:16:43
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.83 kb
#include <bits/stdc++.h>
#define MOD 666013

using namespace std;

typedef vector <vector <long long>> VI;

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

int n;

void product(VI a, VI b, VI &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] = (sol[i][j] + a[i][k] * b[k][j] % MOD) % MOD;
        }
}

int main()
{
    f >> n;

    VI 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;
}