Cod sursa(job #1680586)

Utilizator monicalegendaLegenda Monica monicalegenda Data 8 aprilie 2016 21:31:49
Problema Al k-lea termen Fibonacci Scor 60
Compilator cpp Status done
Runda Arhiva educationala Marime 1.03 kb
#include <iostream>
#include <fstream>
#include <cstring>
#define BEAST 666013

using namespace std;

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

int f[2][2], result[2][2];
int kth;
long long rez;

inline void mult_mat(int a[][2], int b[][2], int c[][2]) {
    int i, j, k;
    for (i = 0; i < 2; i++){
        for (j = 0; j < 2; j++){
            c[i][j] = 0;
            for (k = 0; k < 2; k++){
            	rez = 1LL * a[i][k] * b[k][j] % BEAST;
                c[i][j] += rez;
            }
        }
    }
}

inline void to_power(int power, int result[][2]){
	int aux[2][2];
	result[0][0] = result[1][1] = 1;
	result[0][1] = result[1][0] = 0;
	int i;
	for(i = 0; (1 << i) <= power; i++){
		if(power & (1 << i)){ // bitul i al lui power este 1
			mult_mat(result, f, aux);
			memcpy(result, aux, sizeof(aux));
		}
		mult_mat(f, f, aux);
		memcpy(f, aux, sizeof(aux));
	}
}

int main(){

	fin >> kth;
	f[0][0] = 0;
	f[1][1] = f[1][0] = f[0][1] = 1;
	int result[2][2];
	to_power(kth - 1, result);

	fout << result[1][1] << '\n';

	return 0;	
}