Cod sursa(job #1459230)

Utilizator mouse_wirelessMouse Wireless mouse_wireless Data 9 iulie 2015 14:07:30
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.96 kb
#include <cstdio>

#define MODNR 666013

typedef long long LL;

class Matrix {
private:
	int a[2][2];
public:
	Matrix() {
		a[0][0] = a[0][1] = a[1][0] = a[1][1] = 0;
	}
	Matrix(int x00, int x01, int x10, int x11) {
		a[0][0] = x00; a[0][1] = x01; a[1][0] = x10; a[1][1] = x11;
	}
	LL at(int x, int y) {
		return LL(a[x][y]);
	}
	Matrix operator*(Matrix &M) {
		Matrix aux;
		for (int i = 0; i < 2; i++)
		for (int j = 0; j < 2; j++)
			aux.a[i][j] = (this->at(i, 0) * M.at(0, j) + this->at(i, 1) * M.at(1, j)) % MODNR;
		return aux;
	}
	Matrix pow(int n) {
		if (n == 1)
			return *this;
		Matrix aux = this->pow(n / 2);
		if (n % 2 == 0)
			return aux*aux;
		return aux*aux*(*this);
	}
};

int main() {
	freopen("kfib.in", "r", stdin);
	freopen("kfib.out", "w", stdout);
	int k;
	scanf("%d", &k);
	Matrix M = Matrix(0, 1, 1, 1).pow(k - 2);
	Matrix F(1, 1, 0, 0);
	Matrix Result = F*M;
	printf("%d", int(Result.at(0, 1)));
	return 0;
}