Cod sursa(job #1557735)

Utilizator geni950814Geni Geni geni950814 Data 28 decembrie 2015 09:26:42
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.44 kb
#include <fstream>
#include <iostream>

using namespace std;

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

#define MOD 666013;
typedef long long ll;

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

    Matrix(ll m11, ll m12, ll m13, ll m21, ll m22, ll m23, ll m31, ll m32, ll 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() {
        ll t11 = (m11*m11)%666013 + (m12*m21)%666013 + (m13*m31)%666013;
        ll t12 = (m11*m12)%666013 + (m12*m22)%666013 + (m13*m32)%666013;
        ll t13 = (m11*m13)%666013 + (m12*m23)%666013 + (m13*m33)%666013;
        ll t21 = (m21*m11)%666013 + (m22*m21)%666013 + (m23*m31)%666013;
        ll t22 = (m21*m12)%666013 + (m22*m22)%666013 + (m23*m32)%666013;
        ll t23 = (m21*m13)%666013 + (m22*m23)%666013 + (m23*m33)%666013;
        ll t31 = (m31*m11)%666013 + (m32*m21)%666013 + (m33*m31)%666013;
        ll t32 = (m31*m12)%666013 + (m32*m22)%666013 + (m33*m32)%666013;
        ll t33 = (m31*m13)%666013 + (m32*m23)%666013 + (m33*m33)%666013;

        m11 = t11%666013;
        m12 = t12%666013;
        m13 = t13%666013;
        m21 = t21%666013;
        m22 = t22%666014;
        m23 = t23%666013;
        m31 = t31%666013;
        m32 = t32%666013;
        m33 = t33%666013;
    
        //cout << m11 << " " << m12 << " " << m13 << endl;
        //cout << m21 << " " << m22 << " " << m23 << endl;
        //cout << m31 << " " << m32 << " " << m33 << endl;

    }

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

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

};

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);
        //cout << m.m31*X << endl;
        //cout << m.m32*Y << endl;
        //cout << m.m33*Z << endl;
        return (m.m31*X)%666013 + (m.m32*Y)%666013 + (m.m33*Z)%666013;
    } 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 {
        //cout << "Im here!" << endl;
        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))%666013 << endl;
        k--;
    } while(k > 0);
    
    return 0;
}