Cod sursa(job #2376003)

Utilizator VadimCCurca Vadim VadimC Data 8 martie 2019 13:16:53
Problema Al k-lea termen Fibonacci Scor 75
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.33 kb
#include <iostream>
#include <fstream>

using namespace std;

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

#define NMax 5
#define MOD 666013

struct matrice{
	int n, m;
	int a[NMax][NMax];
	matrice();
//	matrice(const matrice &);
};

matrice::matrice(){
	int i, j;
	for(i = 0; i < NMax; i++)
		for(j = 0; j < NMax; j++)
			a[i][j] = 0;
	n = m = 0;
}

//matrice::matrice(const matrice & x){
//	n = x.n;
//	m = x.m;
//	int i, j;
//	for(i = 0; i < n; i++)
//		for(j = 0; j < m; j++)
//			a[i][j] = x.a[i][j];
//}

matrice operator*(const matrice & a, const matrice & b){
	matrice sol;
	sol.n = a.n;
	sol.m = b.m;
	int i, j, k;
	for(i = 0; i < a.n; i++)
		for(j = 0; j < b.m; j++)
			for(k = 0; k < a.m; k++)
				sol.a[i][j] += (1LL * a.a[i][k] * b.a[k][j]) % MOD;
	return sol;
}

matrice a;
int n;

matrice pow_log(matrice x, int p){
	if(p == 1) return x;
	if(p % 2 == 1) return x * pow_log(x, p - 1);
	matrice nr = pow_log(x, p / 2);
	return nr * nr;
}

int main(){
	a.n = a.m = 2;
	a.a[0][0] = 0;
	a.a[1][0] = 1;
	a.a[0][1] = 1;
	a.a[1][1] = 1;
	matrice b;
	fin >> n;
	b = pow_log(a, n);
	fout << b.a[0][1];
	
//	int i, j;
//	for(i = 0; i < b.n; i++){
//		for(j = 0; j < b.m; j++)
//			cout << b.a[i][j] << ' ';
//		cout << endl;
//	}
}