Cod sursa(job #1355754)

Utilizator sergiunascaSergiu Nasca sergiunasca Data 22 februarie 2015 22:42:13
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.1 kb
#include <stdio.h>
#define M 666013
using namespace std;
long long a,b[5],suma[5],c[5];
int main()
{
    freopen("kfib.in","r",stdin);
    freopen("kfib.out","w",stdout);
    scanf("%lld",&a);
    if(a>1)a=a-1;
    b[1] = 0;
    b[2] = 1;
    b[3] = 1;
    b[4] = 1;
    for(int i=0; (1<<i) <= a; ++i)
    {
        if( ((1<<i)&a)>0 )
        {
            if(suma[4]==0)
            {
                suma[1]=b[1];
                suma[2]=b[2];
                suma[3]=b[3];
                suma[4]=b[4];
            }
            else
            {
                for(int j=1; j<=4; ++j)c[j] = suma[j];
                suma[1] = (c[1]*b[1]+c[2]*b[3])%M;
                suma[2] = (c[1]*b[2]+c[2]*b[4])%M;
                suma[3] = (c[3]*b[1]+c[4]*b[3])%M;
                suma[4] = (c[3]*b[2]+c[4]*b[4])%M;
            }
        }
        for(int j=1; j<=4; ++j)c[j] = b[j];
        b[1] = (c[1]*c[1]+c[2]*c[3])%M;
        b[2] = (c[1]*c[2]+c[2]*c[4])%M;
        b[3] = (c[3]*c[1]+c[4]*c[3])%M;
        b[4] = (c[3]*c[2]+c[4]*c[4])%M;
    }
    printf("%lld",suma[4]);
    return 0;
}