Cod sursa(job #2907438)

Utilizator vladburacBurac Vlad vladburac Data 30 mai 2022 11:25:10
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.84 kb
#include <bits/stdc++.h>
using namespace std;
const int MOD = 666013;
#define int long long

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

void product( int a[2][2], int b[2][2] ) {
  int aux[2][2];
  int i, j, k;
  for( i = 0; i < 2; i++ ) {
    for( j = 0; j < 2; j++ ) {
      aux[i][j] = 0;
      for( k = 0; k < 2; k++ )
        aux[i][j] = ( aux[i][j] + a[i][k] * b[k][j] ) % MOD;
    }
  }
  for( i = 0; i < 2; i++ ) {
    for( j = 0; j < 2; j++ )
      a[i][j] = aux[i][j];
  }
}

int rez[2][2] = { { 0, 1 }, { 1, 1 } };
void lgput( int a[2][2], int put ) {
  while( put ) {
    if( put % 2 == 1 )
      product( rez, a );
    product( a, a );
    put /= 2;
  }
}
int mat[2][2] = { { 0, 1 }, { 1, 1 } };
signed main() {
  int n;
  fin >> n;
  lgput( mat, n - 1 );
  fout << rez[0][1];
  return 0;
}