Cod sursa(job #2869064)

Utilizator alextmAlexandru Toma alextm Data 11 martie 2022 12:17:57
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.75 kb
#include <bits/stdc++.h>
using namespace std;

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

const int MOD = 666013;

struct Matrix {
	int a[2][2];
	Matrix() {
		a[0][0] = a[0][1] = 0;
		a[1][0] = a[1][1] = 0;
	}

	Matrix operator *(const Matrix &other) {
		Matrix res;
		for(int i = 0; i < 2; i++)
			for(int j = 0; j < 2; j++)
				for(int k = 0; k < 2; k++)
					res.a[i][j] = (res.a[i][j] + 1LL * a[i][k] * other.a[k][j]) % MOD;
		return res;
	}
};

Matrix pwr(Matrix M, int n) {
	Matrix res;
	res.a[0][0] = res.a[1][1] = 1;

	while(n != 0) {
		if(n % 2 == 1)
			res = res * M;
		M = M * M;
		n /= 2;
	}
	return res;
}

int main() {
	int n;
	fin >> n;

	Matrix M;
	M.a[0][0] = M.a[0][1] = M.a[1][0] = 1;

	fout << (pwr(M, n).a[1][0]) % MOD << '\n';

	return 0;
}