Pagini recente » Cod sursa (job #2394242) | Cod sursa (job #2103848) | Cod sursa (job #1698021) | Cod sursa (job #1361692) | Cod sursa (job #1680574)
#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];
long long kth;
long long rez;
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 = ((long long)a[i][k]) * b[k][j] % BEAST;
c[i][j] += rez;
}
}
}
}
void to_power(long long power, int result[][2]){
int aux[2][2];
result[0][0] = result[1][1] = 1;
result[0][1] = result[1][0] = 0;
long long 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;
}