Cod sursa(job #1792294)

Utilizator horatiucheval2Horatiu Andrei Cheval horatiucheval2 Data 30 octombrie 2016 12:11:14
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include <iostream>
#include <fstream>

const int MOD = 666013;

//a = b * c
void multiply(int a[3][3], int b[3][3], int c[3][3]){
    int res[3][3];
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            res[i][j] = 0;
        }
    }

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            for(int k = 0; k < 3; k++){
                res[i][j] = res + (b[i][k] * c[k][j]);
            }
        }
    }

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            a[i][j] = res[i][j];
        }
    }
}

// a = b ^ n
void power(int a[3][3], int b[3][3], int n){
    int res[3][3];
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            res[i][j] = (i == j);
        }
    }

    while(n > 0){
        if(n % 2 == 0){
            multiply(b, b, b);
            n /= 2;
        }else{
            multiply(res, res, b);
            n--;
        }
    }

    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            a[i][j] = res[i][j];
        }
    }
}

int main(){
    std::ifstream fin("iepuri.in");
    std::ofstream fout("iepuri.out");


    int t, x, y, z, a, b, c, n;
    int A[3][3];
    fin >> t;
    for(int i = 0; i < t; i++){
        fin >> x >> y >> z >> a >> b >> c >> n;
        A[0][0] = 0;
        A[0][1] = 1;
        A[0][2] = 0;
        A[1][0] = 0;
        A[1][1] = 0;
        A[1][2] = 1;
        A[2][0] = c;
        A[2][1] = b;
        A[2][2] = a;

        int B[3][3];
        power(B, A, n - 2);

        fout << B[2][0] * x + B[2][1] * y + B[2][2] * z << "\n";
    }

    fin.close();
    fout.close();
    return 0;
}