Cod sursa(job #2429773)

Utilizator Mihai145Oprea Mihai Adrian Mihai145 Data 11 iunie 2019 09:08:49
Problema Iepuri Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <fstream>
#include <cstring>

using namespace std;

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

const int MOD = 666013;
const int DIM = 4;

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

struct matrix
{
    int N, M;
    long long a[DIM][DIM];

    matrix()
    {
        N = M = 0;
        memset(a, 0, sizeof(a));
    }

    matrix operator * (const matrix other)
    {
        matrix rez;

        rez.N = N;
        rez.M = other.M;

        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= other.M; j++)
                for(int c = 1; c <= M; c++)
                    rez.a[i][j] = rez.a[i][j] + a[i][c] * other.a[c][j];

        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= M; j++)
                rez.a[i][j] %= MOD;

        return rez;
    }
};

matrix RidPut(int exp)
{
    matrix sol, aux;

    sol.N = sol.M = 3;
    sol.a[1][1] = sol.a[2][2] = sol.a[3][3] = 1;

    aux.N = aux.M = 3;
    aux.a[2][1] = aux.a[3][2] = 1;
    aux.a[1][3] = C, aux.a[2][3] = B, aux.a[3][3] = A;

    for(int i = 1; i <= exp; i <<= 1)
    {
        if(i & exp)
            sol = sol * aux;

        aux = aux * aux;
    }

    return sol;
}

int main()
{
    int T;
    fin >> T;

    matrix m;
    m.N = 1;
    m.M = 3;

    while(T--)
    {
        fin >> X >> Y >> Z >> A >> B >> C >> N;

        m.a[1][1] = X, m.a[1][2] = Y, m.a[1][3] = Z;

        matrix p = RidPut(N - 2);
        m = m * p;

        fout << m.a[1][3] << '\n';
    }

    return 0;
}