Cod sursa(job #1405445)

Utilizator alexandra_udristoiuUdristoiu Alexandra Maria alexandra_udristoiu Data 29 martie 2015 11:30:11
Problema Al k-lea termen Fibonacci Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.11 kb
#include<fstream>
#define mod 666013
using namespace std;
int n;
long long sum;
long long x[3][3];
long long a[3][3] = {
    {0, 0, 0},
    {0, 1, 1},
    {0, 1, 0}
};
long long r[3][3] = {
    {0, 0, 0},
    {0, 1, 0},
    {0, 0, 1}
};
void produs(long long a[3][3], long long b[3][3], long long c[3][3]){
    int n = 2;
    int i, j, k;
    for(i = 1; i <= n; i++){
        for(j = 1; j <= n; j++){
            c[i][j] = 0;
            for(k = 1; k <= n; k++){
                c[i][j] = (c[i][j] + a[i][k] * b[k][j] * 1LL % mod) % mod;
            }
        }
    }
}
void copiere(long long a[3][3], long long b[3][3]){
    for(int i = 1; i <= 2; i++){
        for(int j = 1; j <= 2; j++){
            a[i][j] = b[i][j];
        }
    }
}
ifstream fin("kfib.in");
ofstream fout("kfib.out");
int main(){
    fin>> n;
    n -= 2;
    while(n != 0){
        if(n % 2 == 1){
            produs(a, r, x);
            copiere(r, x);
        }
        produs(a, a, x);
        copiere(a, x);
        n /= 2;
    }
    sum = (r[1][1] + r[1][2]) % mod;
    fout<< sum <<"\n";
    return 0;
}