Cod sursa(job #2334083)

Utilizator Mathe13Mathe Andrei Mathe13 Data 2 februarie 2019 11:01:27
Problema Al k-lea termen Fibonacci Scor 5
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int k;
int lem_atrice[1][2] =
{
	{1, 0}
};

int da_matricea_ma[2][2] =
{
	{1, 1},
	{1, 0}
};

int da_matricea_la_sfarsit[2][2] =
{
	{1, 1},
	{1, 0}
};

void matrix_mul(int matrix_a[][2], int matrix_b[][2], int len_l, int len_c)
{
	int matrix_c[2][2];
	for (int i = 0; i < len_l; ++i)
		for (int j = 0; j < len_c; ++j)
			matrix_c[i][j] = 0;

	for (int i = 0; i < len_l; ++i)
		for (int j = 0; j < len_c; ++j)
			for (int q = 0; q < len_c; ++q){
				matrix_c[i][j] += matrix_a[i][q]*matrix_b[q][j];
				matrix_c[i][j] %= 666013;
			}

	for (int i = 0; i < len_l; ++i)
		for (int j = 0; j < len_c; ++j)
			matrix_a[i][j] = matrix_c[i][j];
}

void Solve()
{
	for (int i = 1; i < k-1; ++i)
		matrix_mul(da_matricea_la_sfarsit, da_matricea_ma, 2, 2);
	matrix_mul(lem_atrice, da_matricea_la_sfarsit, 1, 2);
}

void Read()
{
	fin >> k;
}

void Write()
{
	fout << lem_atrice[0][0];
}

int main()
{
    Read();
    Solve();
    Write();
    return 0;
}