Cod sursa(job #3172364)

Utilizator not_anduAndu Scheusan not_andu Data 20 noiembrie 2023 15:38:30
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <bits/stdc++.h>

using namespace std;

#define INFILE "kfib.in"
#define OUTFILE "kfib.out"

typedef long long ll;

const int MOD = 666013;

ll n, x[2][2], y[2][2];

void mult(ll a[2][2], ll b[2][2]){
    ll c[2][2];
    memset(c, 0, sizeof c);
    for(int i = 0; i < 2; ++i){
        for(int j = 0; j < 2; ++j){
            for(int k = 0; k < 2; ++k){
                c[i][j] += 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];
        }
    }
}

void solve(){

    x[0][1] = x[1][0] = x[1][1] = y[0][0] = y[1][1] = 1;

    cin >> n;

    while(n){
        if(n & 1){
            mult(y, x);
        }
        mult(x, x);
        n >>= 1;
    }

    cout << y[0][1] << '\n';

}

int main(){

    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

    freopen(INFILE, "r", stdin);
    freopen(OUTFILE, "w", stdout);

    solve();

    return 0;
}