Cod sursa(job #2138286)

Utilizator sulzandreiandrei sulzandrei Data 21 februarie 2018 15:27:14
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.97 kb
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("kfib.in");
ofstream out("kfib.out");

struct A
{
    long long mod = 666013;
    long long a[2][2];
    A(){
    }
    A(int v[])
    {
        a[0][0] = v[0];
        a[0][1] = v[1];
        a[1][0] = v[2];
        a[1][1] = v[3];
    }
    A operator *(const A oth)
    {
        A rez;
        for(int i = 0 ; i < 2;  i++)
            for(int j = 0 ; j < 2 ; j++)
            {
                rez.a[i][j] = 0;
                for(int k = 0 ; k < 2 ; k++)
                    rez.a[i][j] = (rez.a[i][j]%mod+ ( (a[i][k]%mod)*(oth.a[k][j]%mod))%mod )%mod;
            }
        return rez;
    }
};
int main()
{
    int k;
    in >> k;
    A z(new int[4]{0,1,1,1});
    k--;
    A res(new int[4]{1,0,0,1});
    A base = z;
    while(k)
    {
        if(k&1)
            res = res*base;
        base = base*base;
        k >>=1;
    }
    out<<res.a[1][1];
    return 0;
}