Cod sursa(job #1146881)

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

#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()
{
    unsigned 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(int i=1;i<k;i++)
        a=a*b;
    cout<<a.m12;
    return 0;
}

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