Cod sursa(job #2544444)

Utilizator RaaaulBaciulescu Raul Raaaul Data 12 februarie 2020 01:36:59
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include <iostream>
#include <fstream>
#define MOD 666013
using namespace std;
ifstream fin ("kfib.in");
ofstream fout ("kfib.out");

struct mat
{
    int m[2][2];
};
mat prod (mat a, mat b)
{
    mat rez;
    rez.m[0][0] = (1LL *a.m[0][0] * b.m[0][0] + 1LL * a.m[0][1] * b.m[1][0]) % MOD;
    rez.m[0][1] = (1LL *a.m[0][0] * b.m[0][1] + 1LL * a.m[0][1] * b.m[1][1]) % MOD;
    rez.m[1][0] = (1LL *a.m[1][0] * b.m[0][0] + 1LL * a.m[1][1] * b.m[1][0]) % MOD;
    rez.m[1][1] = (1LL *a.m[1][0] * b.m[0][1] + 1LL * a.m[1][1] * b.m[1][1]) % MOD;
    return rez;
}
mat pow (int p)
{
    mat r, n;
    r.m[0][0] = 1;
    r.m[0][1] = 0;
    r.m[1][0] = 0;
    r.m[1][1] = 1;

    n.m[0][0] = 0;
    n.m[0][1] = 1;
    n.m[1][0] = 1;
    n.m[1][1] = 1;
    while (p)
    {
        if (p % 2 == 1)
            r = prod (r, n);
        n = prod (n, n);
        p /= 2;
    }

    return r;
}
int main ()
{
    mat r;
    int p, y;
    fin >> p;
    r = pow (p - 1);
    //cout << r.m[0][0] << " " << r.m[0][1] << "\n" << r.m[1][0] << " " << r.m[1][1] << "\n";
    fout << r.m[1][1];
}