Cod sursa(job #1370145)

Utilizator cypry97Dascalitei Ciprian cypry97 Data 3 martie 2015 13:10:17
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.51 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

ifstream in("kfib.in");
ofstream out("kfib.out");

unsigned long long k;
unsigned long long m[2][2];
unsigned long long f1[2]={1,1};
unsigned long long fk[2];
const unsigned long long p = 666013;

void citire()
{
    in>>k;
}

void rezolvare()
{
    unsigned long long m2[2][2]={{0,1},{1,1}};
    unsigned long long aux[2][2];
    m[0][0]=1;
    m[1][1]=1;
    k-=2;
    while(k)
    {
        if(k%2==1)
        {
            aux[0][0]=(m[0][0]*m2[0][0])%p+(m[0][1]*m2[1][0])%p;
            aux[0][1]=(m[0][0]*m2[0][1])%p+(m[0][1]*m2[1][1])%p;
            aux[1][0]=(m[1][0]*m2[0][0])%p+(m[1][1]*m2[1][0])%p;
            aux[1][1]=(m[1][0]*m2[0][1])%p+(m[1][1]*m2[1][1])%p;
            m[0][0]=aux[0][0]%p;
            m[0][1]=aux[0][1]%p;
            m[1][0]=aux[1][0]%p;
            m[1][1]=aux[1][1]%p;
        }
        k=k/2;
        aux[0][0]=(m2[0][0]*m2[0][0])%p+(m2[0][1]*m2[1][0])%p;
        aux[0][1]=(m2[0][0]*m2[0][1])%p+(m2[0][1]*m2[1][1])%p;
        aux[1][0]=(m2[1][0]*m2[0][0])%p+(m2[1][1]*m2[1][0])%p;
        aux[1][1]=(m2[1][0]*m2[0][1])%p+(m2[1][1]*m2[1][1])%p;
        m2[0][0]=aux[0][0]%p;
        m2[0][1]=aux[0][1]%p;
        m2[1][0]=aux[1][0]%p;
        m2[1][1]=aux[1][1]%p;
    }
}

void afisare()
{
    out<<(m[0][1]+m[1][1])%p;
}

int main()
{
    citire();
    rezolvare();
    //cout<<m[0][0]<<' '<<m[0][1]<<'\n'<<m[1][0]<<' '<<m[1][1];
    afisare();
    return 0;
}