Cod sursa(job #1146913)

Utilizator dumytruKana Banana dumytru Data 19 martie 2014 13:39:33
Problema Al k-lea termen Fibonacci Scor 5
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <utility>

#define MOD 666013
#define tip long long
#define matrix pair<pair<tip,tip>,pair<tip,tip> >
#define m11 first.first
#define m12 first.second
#define m21 second.first
#define m22 second.second

using namespace std;

matrix a,b;
matrix operator*(matrix a, matrix b);

int main()
{
    tip k;
    ifstream f("kfib.in");
    ofstream g("kfib.out");
    a.m11 = 0; a.m12 = 1;
    a.m21 = 1; a.m22 = 1;
    b.m11 = 0; b.m12 = 1;
    b.m21 = 1; b.m22 = 1;
    f>>k;
    for(tip i=1;i<k;i++)
        a=a*b;
    g<<a.m12;
    return 0;
}

matrix operator*(matrix a, matrix b)
{
    matrix x;
    x.m11 = (a.m11*b.m11 + a.m12*b.m21)%MOD;
    x.m12 = (a.m11*b.m12 + a.m12*b.m22)%MOD;
    x.m21 = (a.m21*b.m21 + a.m22*b.m21)%MOD;
    x.m22 = (a.m21*b.m21 + a.m22*b.m22)%MOD;
    return x;
}