Cod sursa(job #1795225)

Utilizator Vlad_317Vlad Panait Vlad_317 Data 2 noiembrie 2016 09:08:21
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.89 kb
#include <cstdio>

using namespace std;

const int MOD =  666013;

struct matrice
{
    long long a, b, c, d;
};

matrice inmultire(matrice x, matrice y)
{
    matrice z;
    z.a = (x.a * y.a % MOD + x.b * y.c % MOD) % MOD;
    z.b = (x.a * y.b % MOD + x.b * y.d % MOD) % MOD;
    z.c = (x.c * y.a % MOD + x.d * y.c % MOD) % MOD;
    z.d = (x.c * y.b % MOD + x.d * y.d % MOD) % MOD;
    return z;

}

matrice lgput(matrice x, int pow)
{
    if(pow == 1)
        return x;
    matrice y = lgput(x, pow / 2);
    if(pow % 2 == 1)
        return inmultire(inmultire(y, y), x);
    return inmultire(y, y);
}


int main()
{
    FILE *fin, *fout;

    fin = fopen("kfib.in", "r");
    fout = fopen("kfib.out", "w");

    int k;

    fscanf(fin, "%d", &k);

    matrice M;
    M.a = 0;
    M.b = 1;
    M.c = 1;
    M.d = 1;

    M = lgput(M, k - 2);

    fprintf(fout, "%lld", (1LL * M.b + M.d) % MOD);

    return 0;
}