Cod sursa(job #1609522)

Utilizator razvandRazvan Dumitru razvand Data 22 februarie 2016 20:49:39
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.6 kb
#include <iostream>
#include <fstream>
#define MOD 666013

using namespace std;

ifstream in("iepuri.in");
ofstream out("iepuri.out");

struct matrix {
    long long m[3][3];
    matrix operator*(const matrix &x) {
        matrix temp;
        int cnt = 0;
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                for(int k = 0; k < 3; k++)
                    if(cnt == 0)
                        temp.m[i][j] = m[i][cnt]*x.m[cnt++][j]/*%MOD*/;
                    else
                        temp.m[i][j] += m[i][cnt]*x.m[cnt++][j]/*%MOD*/;
                temp.m[i][j] %= MOD;
                cnt = 0;
            }
        }
        return temp;
    }
};

matrix lgpow(matrix st, int pw, matrix init) {

    if(pw == 1)
        return st;
    if(pw == 2)
        return st*st;
    if(pw%2 == 0) {
        matrix temp = lgpow(st, pw/2, init);
        return temp*temp;
    } else {
        return lgpow(st, pw-1, init)*init;
    }

}

matrix st;
matrix mul;

int main() {

    int t,X,Y,Z,A,B,C,N;
    in >> t;

    for(int i = 0; i < t; i++) {

        in >> X >> Y >> Z >> A >> B >> C >> N;

        st.m[0][0] = X;
        st.m[0][1] = Y;
        st.m[0][2] = Z;

        mul.m[0][0] = 0;
        mul.m[0][1] = 0;
        mul.m[0][2] = C;
        mul.m[1][0] = 1;
        mul.m[1][1] = 0;
        mul.m[1][2] = B;
        mul.m[2][0] = 0;
        mul.m[2][1] = 1;
        mul.m[2][2] = A;

        mul = lgpow(mul, N-2, mul);
        mul = st*mul;

        out << mul.m[0][2]%MOD << '\n';

    }

    return 0;
}