Cod sursa(job #924043)

Utilizator peter_aPeter Agnes peter_a Data 24 martie 2013 07:53:12
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#define R9 666013
#define NM 10

using namespace std;

long long i, j, k, n, m, a[NM][NM], b[NM][NM], c[NM][NM];
ifstream fin("kfib.in");
ofstream fout("kfib.out");

void inmultire(long long a[NM][NM], long long b[NM][NM], long long c[NM][NM]) {
    long long i, j, k;
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++) {
            c[i][j] = 0;
            for (k = 0; k < n; k++)
                c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % R9;
        }
}

void atribuire(long long a[NM][NM], long long b[NM][NM]) {
    long long i, j;
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            a[i][j] = b[i][j];
}

int main() {
    n = 2;
    fin >> m;
    fin.close();
    m -= 2;
    c[0][0] = c[1][1] = 1;
    c[1][0] = c[0][1] = 0;
    a[0][0] = 0;
    a[0][1] = a[1][0] = a[1][1] = 1;

    while (m) {
        if (m % 2) {
            inmultire(a, c, b);
            atribuire(c, b);
        }
        inmultire(a, a, b);
        atribuire(a, b);
        m /= 2;
    }
    fout << (c[1][0] + c[1][1]) % R9;
    return 0;
}