Cod sursa(job #2978085)

Utilizator DKMKDMatei Filibiu DKMKD Data 12 februarie 2023 21:46:34
Problema Al k-lea termen Fibonacci Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <fstream>

using namespace std;

ifstream fin("test.in");
ofstream fout("test.out");

const int mod = 666013;

void produs(int a[][2], int b[][2], int m[][2])
{
    m[0][0] = (1LL * a[0][0] * b[0][0] + 1LL * a[0][1] * b[1][0]) % mod;
    m[0][1] = (1LL * a[0][0] * b[0][1] + 1LL * a[0][1] * b[1][1]) % mod;
    m[1][0] = (1LL * a[1][0] * b[0][0] + 1LL * a[1][1] * b[1][0]) % mod;
    m[1][1] = (1LL * a[1][0] * b[0][1] + 1LL * a[1][1] * b[1][1]) % mod;
}

void atribuire(int a[][2], int b[][2])
{
    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            a[i][j] = b[i][j];
}

void putere(int n, int unit[][2], int init[][2], int sol[][2])
{
    while (n > 0)
    {
        if (n & 1)
        {
            produs(unit, init, sol);
            atribuire(unit, sol);
        }

        produs(init, init, sol);
        atribuire(init, sol);

        n /= 2;
    }
}

int k, unit[2][2], init[2][2], sol[2][2];

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(0);

    fin >> k;
    fin.close();

    unit[0][0] = 1, unit[0][1] = 0, unit[1][0] = 0, unit[1][1] = 1;
    init[0][0] = 1, init[0][1] = 1, init[1][0] = 1, init[1][1] = 0;

    putere(k, unit, init, sol);

    fout << unit[0][1];

    fout.close();
    return 0;
}