Cod sursa(job #643118)

Utilizator mihaibogdan10Mihai Bogdan mihaibogdan10 Data 2 decembrie 2011 23:22:17
Problema Al k-lea termen Fibonacci Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include<cstdio>
#define MOD 666013
#define ll long long
using namespace std;

class matrice{
	public: ll a, b, c, d;
	
	matrice (){
		a = b = c = d = 0;
	}
	matrice (ll a1, ll b1, ll c1, ll d1){
		this->a = a1;
		this->b = b1;
		this->c = c1;
		this->d = d1;
	}
	
	matrice operator * (matrice other){
		matrice m;
		m.a = (this->a * other.a + this->b * other.c) % MOD;
		m.b = (this->a * other.b + this->b * other.d) % MOD;
		m.c = (this->c * other.a + this->d * other.c) % MOD;
		m.d = (this->c * other.b + this->d * other.d) % MOD;
		return m;
	}
};

matrice X (0, 1, 1, 1);
matrice Pow(int p){
	if (p == 1) return X;
	if (p % 2 == 1) return X * Pow(p-1);
	return Pow(p/2) * Pow(p/2);
}

int main(){
	int k;
	freopen ("kfib.in", "r", stdin), freopen("kfib.out", "w"	, stdout);	
	scanf ("%d", &k);
	matrice M = Pow(k - 1);
	printf("%lld\n", M.d);
	return 0;
}