Cod sursa(job #2572649)

Utilizator RazvanPanaiteRazvan Panaite RazvanPanaite Data 5 martie 2020 13:42:09
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp-64 Status done
Runda OJI 2020 Marime 1.61 kb
#include <bits/stdc++.h>
#define pb push_back

using namespace std;

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

void debug_out() { cerr << '\n'; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}
#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

typedef pair<int,int> pii;
typedef long long int ll;
typedef long double ld;

const int MOD = 666013;

struct nume{
    int mat[2][2];
};

nume init,null;

inline nume operator*(nume x,nume y){
    nume ans;
    int i,j,k;
    for(i=0;i<2;i++)
        for(j=0;j<2;j++){
            ans.mat[i][j]=0;
            for(k=0;k<2;k++)
                ans.mat[i][j]=(ans.mat[i][j]+1LL*x.mat[i][k]*y.mat[k][j]%MOD)%MOD;
        }
    return ans;
}

int n;

void constr();
nume explog(nume x,int n);

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t,i,j;
    int x,y,z;

    fin>>n;
    if(n == 0){
        fout<<"0\n";
        return 0;
    }
    if(n <= 2){
        fout<<"1\n";
        return 0;
    }
    constr();
    nume ans=explog(init,n-2);
    fout<<(ans.mat[0][0]+ans.mat[1][0])%MOD<<'\n';

    return 0;
}
nume explog(nume x,int n){
    if(!n)
        return null;
    if(n & 1)
        return x*explog(x*x,n/2);
    return explog(x*x,n/2);
}
void constr(){
    int i;
    for(i=0;i<2;i++){
        null.mat[i][i]=1;
        init.mat[i][0]=1;
    }
    init.mat[0][1]=1;
}