Cod sursa(job #1540918)

Utilizator GrandmasterSoucup Bogdan Grandmaster Data 3 decembrie 2015 15:03:49
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.08 kb
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <algorithm>
#include <cstring>
#include <map>
#include <iomanip>
#include <time.h>
#include <stdio.h>
//#include <iostream>
using namespace std;
ifstream cin("kfib.in");
ofstream cout("kfib.out");
int MOD = 666013;
struct matrice{
    unsigned long long a, b, c, d;
};
matrice x, y, z, nullm, cop;
matrice inm(matrice x, matrice y)
{
    matrice z;
    z.a = (x.a * y.a + x.b * y.c) % MOD;
    z.b = (x.a * y.b + x.b * y.d) % MOD;
    z.c = (x.c * y.a + x.d * y.c) % MOD;
    z.d = (x.c * y.b + x.d * y.d) % MOD;
    return z;
}
matrice putere(matrice x, int e, matrice nullm)
{
    if(e == 0)
        return nullm;
    if(e % 2 == 0){
        matrice gov = putere(x, e / 2, nullm);
        return inm(gov, gov);
    }
    else
        return inm(x, putere(x, e - 1, nullm));
}
int main(){
    int v;
    cin >> v;
    x.a = 0;x.b = 1;
    x.c = 1;x.d = 1;
    nullm.a = 1;nullm.b = 0;
    nullm.c = 0;nullm.d = 1;
    cop = putere(x, v, nullm);
    cout << cop.b;
    return 0;
}