Cod sursa(job #1211321)

Utilizator andrei_diaconuAndrei Diaconu andrei_diaconu Data 22 iulie 2014 13:04:04
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <string.h>
#define MOD 666013
using namespace std;
ifstream f("kfib.in");
ofstream g("kfib.out");
int k, i, j, l;
long long res[3][3], cmat[3][3], put, tmp[3][3];
int main()
{
    f>>k;
    cmat[1][1]=0;
    cmat[1][2]=1;
    cmat[2][1]=1;
    cmat[2][2]=1;
    put=k-1;
    res[1][1]=1;
    res[2][2]=1;
    while (put!=0) {
        if (put%2==1) {
            for (i=1; i<=2; i++)
                for (j=1; j<=2; j++)
                    for (l=1; l<=2; l++)
                        tmp[i][j]=(tmp[i][j] + res[i][l]*cmat[l][j])%MOD;
            memcpy(res, tmp, sizeof(tmp));
        }
        memset(tmp, 0, sizeof(tmp));
        for (i=1; i<=2; i++)
            for (j=1; j<=2; j++)
                for (l=1; l<=2; l++)
                    tmp[i][j]=(tmp[i][j] + cmat[i][l]*cmat[l][j])%MOD;
        memcpy(cmat, tmp, sizeof(tmp));
        memset(tmp, 0, sizeof(tmp));
        put/=2;
    }
    g<<res[2][2];
    return 0;
}