Cod sursa(job #1557729)

Utilizator geni950814Geni Geni geni950814 Data 28 decembrie 2015 02:48:38
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.7 kb
#include <fstream>
using namespace std;

int A, B, C, X, Y, Z, N;

struct Matrix {
    int m11, m12, m13, m21, m22, m23, m31, m32, m33;

    Matrix(int m11, int m12, int m13, int m21, int m22, int m23, int m31, int m32, int m33) {
        this->m11 = m11;
        this->m12 = m12;
        this->m13 = m13;
        this->m21 = m21;
        this->m22 = m22;
        this->m23 = m23;
        this->m31 = m31;
        this->m32 = m32;
        this->m33 = m33;
    }
    
    void MatrixSquare() {
        int t11 = m11*m11 + m12*m21 + m13*m31;
        int t12 = m11*m12 + m12*m22 + m13*m32;
        int t13 = m11*m13 + m12*m23 + m13*m33;
        int t21 = m21*m11 + m22*m21 + m23*m31;
        int t22 = m21*m12 + m22*m22 + m23*m32;
        int t23 = m21*m13 + m22*m23 + m23*m33;
        int t31 = m31*m11 + m32*m21 + m33*m31;
        int t32 = m31*m12 + m32*m22 + m33*m32;
        int t33 = m31*m13 + m32*m23 + m33*m33;

        m11 = t11;
        m12 = t12;
        m13 = t13;
        m21 = t21;
        m22 = t22;
        m23 = t23;
        m31 = t31;
        m32 = t32;
        m33 = t33;
    }

    
    void MatrixMult(Matrix& other) {
        int t11 = m11*other.m11 + m12*other.m21 + m13*other.m31;
        int t12 = m11*other.m12 + m12*other.m22 + m13*other.m32;
        int t13 = m11*other.m13 + m12*other.m23 + m13*other.m33;
        int t21 = m21*other.m11 + m22*other.m21 + m23*other.m31;
        int t22 = m21*other.m12 + m22*other.m22 + m23*other.m32;
        int t23 = m21*other.m13 + m22*other.m23 + m23*other.m33;
        int t31 = m31*other.m11 + m32*other.m21 + m33*other.m31;
        int t32 = m31*other.m12 + m32*other.m22 + m33*other.m32;
        int t33 = m31*other.m13 + m32*other.m23 + m33*other.m33;

        m11 = t11;
        m12 = t12;
        m13 = t13;
        m21 = t21;
        m22 = t22;
        m23 = t23;
        m31 = t31;
        m32 = t32;
        m33 = t33;
    }

};

Matrix MatrixPow(int);


int Iepuri(int A, int B, int C, int X, int Y, int Z, int N) {
    if(N-2 > 0) {
        Matrix m = MatrixPow(N-2);
        return m.m31*X + m.m32*Y + m.m33*Z;
    } else if(!N) {
        return X;
    } else if(N == 1) {
        return Y;
    } else {
        return Z;
    }
}

Matrix MatrixPow(int n) {
     Matrix m(0, 1, 0, 0, 0, 1, C, B, A);
     if(n == 1) {

        return m;
    } 
    Matrix half = MatrixPow(n/2);
    if(n%2) {
        half.MatrixSquare();
        half.MatrixMult(m);
    } else {
        half.MatrixSquare();
    }
    return half;
}


int main() {
    
    ifstream f("iepuri.in");
    ofstream g("iepuri.out");

    int k;
    f >> k;
    
    do {
        f >> X >> Y >> Z >> A >> B >> C >> N;
        g << Iepuri(A, B, C, X, Y, Z, N) << endl;
        k--;
    } while(k > 0);
    
    return 0;
}