Cod sursa(job #1084020)

Utilizator mihai.cuculiciCuculici Mihail mihai.cuculici Data 16 ianuarie 2014 17:08:51
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <vector>
#define REP(i,n) for (int i = 1; i <= n; i++)
#include <iostream>
#include <fstream>
using namespace std;

typedef long long ll;
typedef vector<vector<ll> > matrix;
const ll MOD = 666013;
const int K = 2;
ifstream f ("kfib.in");
ofstream g ("kfib.out");
int n,i,a;

// computes A * B
matrix mul(matrix A, matrix B)
{
    matrix C(K+1, vector<ll>(K+1));
    REP(i, K) REP(j, K) REP(k, K)
        C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
    return C;
}

// computes A ^ p
matrix pow(matrix A, int p)
{
    if (p == 1)
        return A;
    if (p % 2)
        return mul(A, pow(A, p-1));
    matrix X = pow(A, p/2);
    return mul(X, X);
}

// returns the N-th term of Fibonacci sequence
int fib(int N)
{
    // create vector F1
    vector<ll> F1(K+1);
    F1[1] = 1;
    F1[2] = 1;

    // create matrix T
    matrix T(K+1, vector<ll>(K+1));
    T[1][1] = 0, T[1][2] = 1;
    T[2][1] = 1, T[2][2] = 1;

    // raise T to the (N-1)th power
    if (N == 1)
        return 1;
    T = pow(T, N-1);

    // the answer is the first row of T . F1
    ll res = 0;
    REP(i, K)
        res = (res + T[1][i] * F1[i]) % MOD;
    return res;
}

int main()
{
    f>>n;
    g<<fib(n)<<"\n";
    f.close();
    g.close();
    return 0;
}