Cod sursa(job #1116410)

Utilizator alexalghisiAlghisi Alessandro Paolo alexalghisi Data 22 februarie 2014 15:39:02
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>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>


#define DN 10
#define MOD 666013
#define pb push_back
#define mp make_pair
#define per pair<int,int>
#define INF (1<<30)
#define LL long long
#define un unsigned
#define x first
#define y second
using namespace std;

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


void pw(int p)
{
    LL rez[3][3],aux[3][3],tmp[3][3];
    rez[1][1]=1; rez[1][2]=0;
    rez[2][1]=0; rez[2][2]=1;

    aux[1][1]=0; aux[1][2]=1;
    aux[2][1]=1; aux[2][2]=1;

    for(int bit=0;bit<=31;++bit){

        if( (p>>bit) &1 ){

            tmp[1][1]=0; tmp[1][2]=0;
            tmp[2][1]=0; tmp[2][2]=0;

            for(int i=1;i<=2;++i)
                for(int j=1;j<=2;++j)
                    for(int k=1;k<=2;++k)
                        tmp[i][j] += ( rez[i][k] * aux[k][j] ) % MOD;

            for(int i=1;i<=2;++i)
                for(int j=1;j<=2;++j)
                    rez[i][j]=tmp[i][j];
        }

        tmp[1][1]=0; tmp[1][2]=0;
        tmp[2][1]=0; tmp[2][2]=0;
        for(int i=1;i<=2;++i)
                for(int j=1;j<=2;++j)
                    for(int k=1;k<=2;++k)
                        tmp[i][j] += ( aux[i][k] * aux[k][j] ) % MOD;

        for(int i=1;i<=2;++i)
            for(int j=1;j<=2;++j)
                aux[i][j]=tmp[i][j];
    }
    g<<(rez[1][2]+rez[2][2])%MOD;
}

int main()
{
    int n;
    f>>n;
    pw(n-2);

    return 0;
}