Cod sursa(job #2429780)

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

using namespace std;

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

const int MOD = 666013;
const int DIM = 5;

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

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

    matrix()
    {
        N = M = 0;

        for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
                a[i][j] = 0;
    }

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

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

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

        return rez;
    }
};

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

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

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

    for(long long 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;

        if(N <= 2)
        {
            if(N == 0)
                fout << X << '\n';
            else if(N == 1)
                fout << Y << '\n';
            else
                fout << Z << '\n';

            continue;
        }

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

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

        fout << m.a[0][2] << '\n';
    }

    fin.close();
    fout.close();

    return 0;
}