Cod sursa(job #1111058)

Utilizator predator5047Butiu Alexandru Octavian predator5047 Data 18 februarie 2014 16:48:18
Problema Al k-lea termen Fibonacci Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
// ConsoleApplication4.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

#define O2 {{0, 0}, {0, 0}}
typedef int m2x2[2][2];
typedef int m1x2[1][2];

void mul(m2x2 a, m2x2 b, m2x2 c) {
	m2x2 aux = O2;
	for (int i = 0; i < 2; ++i)
	for (int j = 0; j < 2; ++j)
	for (int k = 0; k < 2; ++k) {
		aux[i][j] = (aux[i][j] + 1ULL * a[i][k] * b[k][j]) % 666013;
	}
	memcpy(c, aux, sizeof(m2x2));
}

void log_pow(m2x2 x, int n) {
	if (n <= 1)
		return;
	if (n % 2 == 0) {
		mul(x, x, x);
		log_pow(x, n / 2);
	} else {
		m2x2 aux;
		memcpy(aux, x, sizeof(m2x2));
		mul(x, x, x);
		log_pow(x, (n - 1) / 2);
		mul(x, aux, x);
	}

}

int main() {
	freopen("kfib.in", "r", stdout);
	freopen("kfib.out", "w", stdout);
	m2x2 a = {
		{ 1, 1 },
		{ 1, 0 }
	};
	int k;
	cin >> k;

	log_pow(a, k - 1);
	cout << a[0][0] << "\n";

	fclose(stdout);
	fclose(stdin);
	
	return 0;
}