Cod sursa(job #3371902)

Utilizator Andrei1209Andrei Mircea Andrei1209 Data 1 noiembrie 2026 17:31:12
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include <fstream>

using namespace std;
ifstream fin("kfib.in");
ofstream fout("kfib.out");
const int MOD = 666013;
void inmulteste( int a[3][3], int b[3][3], int n, int m, int p, int result[3][3] )
{
    int i, j, k;
    for ( i = 1; i <= n; ++i )
        for ( j = 1; j <= p; ++j )
        {
            result[i][j] = 0;
            for ( k = 1; k <= m; ++k )
                result[i][j] = ( result[i][j] + 1LL * a[i][k] * b[k][j] % MOD ) % MOD;
        }
}
void exp(int a[3][3], int n, int putere, int result[3][3])
{
    int i, j;
    if ( putere == 0 )
    {
        for ( i = 1; i <= n; ++i )
            for ( j = 1; j <= n; ++j )
                result[i][j] = (i == j );
    }
    else
    {
        if ( putere % 2 == 0 )
        {
            int aux[3][3];
            exp( a, n, putere / 2, aux );
            inmulteste(aux, aux, n, n, n, result );
        }
        else
        {
            int aux[3][3];
            exp(a, n, putere -1, aux);
            inmulteste(aux, a, n, n, n, result);
        }
    }
}
int main()
{
    int k;
    fin >> k;
    int a[3][3], b[2][3], result[3][3], aux[3][3];
    a[1][1] = 0, a[1][2] = 1, a[2][1] = 1, a[2][2] = 1;
    b[1][1] = 0, b[1][2] = 1;

    exp( a, 2, k, aux );
    inmulteste(b, aux, 1, 2, 2, result);
    fout << result[1][1];
    return 0;
}