Cod sursa(job #2150038)

Utilizator cipri321Marin Ciprian cipri321 Data 3 martie 2018 11:16:20
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.84 kb
#include <fstream>
#define MOD 666013
using namespace std;
ifstream fi("kfib.in");
ofstream fo("kfib.out");
int n;
int A[2][2]={{0,1},{1,1}};
void mult(int A[2][2],int B[2][2])
{
    int AUX[2][2]={{0,0},{0,0}};
    for(int i=0;i<=1;i++)
        for(int j=0;j<=1;j++)
            for(int k=0;k<=1;k++)
                AUX[i][j]=(1LL*AUX[i][j]+1LL*A[i][k]*B[k][j])%MOD;
    for(int i=0;i<=1;i++)
        for(int j=0;j<=1;j++)
            A[i][j]=AUX[i][j];
}
void exp(int A[2][2],int n)
{
    int REZ[2][2]={{1,0},{0,1}};
    while(n)
    {
        if(n&1)
            mult(REZ,A);
        mult(A,A);
        n>>=1;
    }
    for(int i=0;i<=1;i++)
        for(int j=0;j<=1;j++)
            A[i][j]=REZ[i][j];
}
int main()
{
    fi>>n;
    exp(A,n-1);
    fo<<A[1][1];
    fi.close();
    fo.close();
    return 0;
}