Cod sursa(job #2274961)

Utilizator tigeraOprea Tereza Emilia tigera Data 2 noiembrie 2018 18:10:52
Problema Al k-lea termen Fibonacci Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.08 kb
//
//  main.cpp
//  noiembrie
//
//  Created by Tereza Oprea on 02/11/2018.
//  Copyright © 2018 Tereza Oprea. All rights reserved.
//

#include <iostream>
#define M 666013
#include <fstream>

using namespace std;

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

int k, a[5][5];

void inmultire (int a[5][5])
{
    int x, y, z, t;
    x = a[1][1];
    y = a[1][2];
    z = a[2][1];
    t = a[2][2];
    a[1][1] = (x*x + y*z) %M;
    a[1][2] = (x*y + y*t) %M;
    a[2][1] = (z*x + t*z) %M;
    a[2][2] = (z*y + t*t) %M;
}

void putere (int a[5][5], int n)
{
    if (n == 1)
        return;
    if (n%2 == 0)
    {
        putere (a, n/2);
        inmultire (a);
    }
    else
    {
        putere (a, n/2);
        inmultire (a);
        int x, y, z, t;
        x = a[1][1];
        y = a[1][2];
        z = a[2][1];
        t = a[2][2];
        a[1][1] = z;
        a[1][2] = t;
        a[2][1] = (x + z) %M;
        a[2][2] = (y + t) %M;
        
    }
}

int main() {
    fin >> k;
    a[1][2] = 1;
    a[2][1] = 1;
    a[2][2] = 1;
    putere (a, k-2);
    fout << (a[1][2] + a[2][2]) %M;
}