Cod sursa(job #2573359)

Utilizator PopeangaMihneaPopeanga Mihnea- Stefan PopeangaMihnea Data 5 martie 2020 17:17:08
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;

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

int n;
int Mat[3][3], SOL[3][3];
const int MOD = 666013;

void mult(int a[][3], int b[][3], int c[][3])
{
    for(int i = 1; i <= 2; ++i)
    {
        for(int j = 1; j <= 2; ++j)
        {
            for(int k = 1; k <= 2; ++k)
            {
                c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % MOD;
            }
        }
    }
}

void lg_put(int n, int M[][3])
{
    int C[3][3], aux[3][3];
    memcpy(C, Mat, sizeof(Mat));
    M[1][1] = M[2][2] = 1;
    while(n > 0)
    {
        if(n % 2 == 1)
        {
            --n;
            memset(aux, 0, sizeof(aux));
            mult(M, C, aux);
            memcpy(M, aux, sizeof(aux));
        }
        memset(aux, 0, sizeof(aux));
        mult(C, C, aux);
        memcpy(C, aux, sizeof(C));
        n = n / 2;
    }
}

int main()
{
    fin >> n;

    Mat[1][1] = 0;
    Mat[1][2] = 1;
    Mat[2][1] = 1;
    Mat[2][2] = 1;

    lg_put(n - 1, SOL);

    fout << SOL[2][2];
    return 0;
}