Cod sursa(job #3197583)

Utilizator TomaBToma Brihacescu TomaB Data 27 ianuarie 2024 10:23:43
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <iostream>
#include <fstream>
using namespace std;

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

int Sol[2][2], M[2][2];

const int MOD = 666013;

void Multiply (int A[2][2], int B[2][2])
{
    long long C[2][2];

    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
        {
C[i][j] = 0;
for (int k = 0; k < 2; k++)
            {

                C[i][j] += 1LL*A[i][k]*B[k][j];
            }
            C[i][j] %= MOD;
        }


    for (int i = 0; i < 2; i++)
        for (int j = 0; j < 2; j++)
            A[i][j] = C[i][j];

}

int main()
{

    // + problema cover1 de pe campion

    int k;
    fin >> k;
    Sol[0][1] = 1;
    M[0][1]=M[1][1]=M[1][0]=1;
    while (k)
    {
        if (k % 2 == 1)
            Multiply(Sol, M);
        Multiply(M,M);
        k = k / 2;
    }

    fout << Sol[0][0] << "\n";
    return 0;
}