Cod sursa(job #2665689)

Utilizator AntoniuFicAntoniu Ficard AntoniuFic Data 31 octombrie 2020 11:14:26
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.13 kb
#include <fstream>
#define MOD 666013

using namespace std;

void matrixMultiplication33(int original[][3], int other[][3]){
    int o[3][3] = {{other[0][0], other[0][1], other[0][2]}, {other[1][0], other[1][1], other[1][2]}, {other[2][0], other[2][1], other[2][2]}};
    other[0][0] = other[0][1] = other[0][2] = other[1][0] = other[1][1] = other[1][2] = other[2][0] = other[2][1] = other[2][2] = 0;
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            for (int k = 0; k < 3; ++k) {
                other[i][j] += (original[i][k] * o[k][j]) % MOD;
            }
        }
    }
}

void matrixMultiplication13(int original[][3], int other[][3]){
    int o[1][3] = {{other[0][0], other[0][1], other[0][2]}};
//    other[0][0] = ((1LL * original[0][0] * o[0][0]) + (1LL * original[1][0] * o[0][1]) + (1LL * original[2][0] * o[0][2])) % MOD;
//    other[0][1] = ((1LL * original[0][1] * o[0][0]) + (1LL * original[1][1] * o[0][1]) + (1LL * original[2][1] * o[0][2])) % MOD;
    other[0][2] = ((original[0][2] * o[0][0]) + (original[1][2] * o[0][1]) + (original[2][2] * o[0][2])) % MOD;
}

void matrixPower(int original[][3], int pow, int mat[][3]){
    for(int i=0; (1<<i)<=pow; i++){
        if(((1<<i)&pow)>0)
            matrixMultiplication33(original, mat);
        int orig[3][3] = {{original[0][0], original[0][1], original[0][2]}, {original[1][0], original[1][1], original[1][2]}, {original[2][0], original[2][1], original[2][2]}};
        matrixMultiplication33(orig, original);
    }
}

int main() {
    int t;
    ifstream f("iepuri.in");
    ofstream g("iepuri.out");
    f>>t;
    for (int i = 0; i < t; ++i) {
        int n, a, b, c, x, y, z;
        f>>x>>y>>z>>a>>b>>c>>n;
        n--;
        int theMatrix[3][3] = {{0, 0, c}, {1, 0, b}, {0, 1, a}};
        int mat[3][3] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};
        matrixPower(theMatrix, n - 1, mat);
//    for (int i = 0; i < 3; ++i) {
//        for (int j = 0; j < 3; ++j) {
//            cout<<mat[i][j]<<' ';
//        }
//        cout<<endl;
//    }
        int elem[1][3] = {{x, y, z}};
        matrixMultiplication13(mat, elem);
        g<<elem[0][2]<<'\n';
    }

    return 0;
}