Cod sursa(job #2949140)

Utilizator IanDoIan Dontu IanDo Data 29 noiembrie 2022 23:01:12
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.9 kb
#include <fstream>

using namespace std;

const int MOD = 666013;

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

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

void putere(int a [2][2], int n, int p [2][2]) {
    while (n != 0) {
        if (n % 2 != 0) {
            produs(p, a);
        }
        produs(a, a);
        n /= 2;
    }
}

int main() {
    int k;
    fin >> k;
    int p [2][2] = {{1, 0}, {0, 1}}, a [2][2] = {{1, 1}, {1, 0}};
    putere(a, k - 1, p);
    fout << p [0][0] << " ";
}