Cod sursa(job #1957565)

Utilizator TimoteiCopaciu Timotei Timotei Data 7 aprilie 2017 16:59:14
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.83 kb
#include <iostream>
#include <fstream>
#define mod 666013
#define S(x, y) ((x + y) % mod)
#define P(x, y) ((1LL*x * y) % mod)
using namespace std;

int k;
typedef struct {
    int x1, x2, x3, x4;
} matrix;
matrix A, Z = {0, 1, 1, 1}, I = {1, 0, 0, 1};
matrix produs(matrix A, matrix B)
{
   matrix C = {S(P(A.x1, B.x1), P(A.x2, B.x3)), S(P(A.x1, B.x2), P(A.x2, B.x4)), S(P(A.x3, B.x1), P(A.x4, B.x3)), S(P(A.x3, B.x2), P(A.x4, B.x4))};
   return C;
}
matrix solve(matrix A, int i)
{
   if(i == 0) return I;
   if(i % 2 == 0){
      A = produs(A, A);
      return solve(A, i / 2);
   }
    else {
        I = produs(I, A);
        return solve(A, i - 1);
     }
}
int main()
{
    ifstream fin("kfib.in");
    ofstream fout("kfib.out");
    fin >> k;
    A = solve(Z, k - 1);
    fout << A.x4;
    return 0;
}