Cod sursa(job #2368566)

Utilizator petru.ciocirlanPetru Ciocirlan petru.ciocirlan Data 5 martie 2019 16:36:22
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <fstream>
#include <cstring>
using namespace std;

#define FILE_NAME "iepuri"
ifstream in (FILE_NAME".in");
ofstream out(FILE_NAME".out");

const long long MOD = 666013;

void multMat(long long A[3][3], long long B[3][3])
{
    long long C[3][3] = {};
    for(int i = 0; i < 3; ++i)
        for(int j = 0; j < 3; ++j)
            for(int k = 0; k < 3; ++k)
                C[i][j] = (C[i][j] + 1LL * A[i][k] * B[k][j] % MOD) % MOD;

    memcpy(A, C, 3 * 3 * sizeof(long long));
}

void powMat(long long M[3][3], int exp)
{
    long long Result[3][3] = {{1, 0, 0},
                        {0, 1, 0},
                        {0, 0, 1}};
    while(exp)
    {
        if(exp&1)
            multMat(Result, M);
        multMat(M, M);
        exp >>= 1;
    }

    memcpy(M, Result, 3 * 3 * sizeof(long long));
}

int main()
{
    long long T;
    in >> T;
    while(T--)
    {
        long long X, Y, Z, A, B, C, N;
        in >> X >> Y >> Z;
        in >> A >> B >> C;
        long long M[3][3] = {{0, 0, C},
                       {1, 0, B},
                       {0, 1, A}};
        in >> N;
        powMat(M, N-2);
        out << ((X * M[0][2] % MOD + Y * M[1][2] % MOD) % MOD + Z * M[2][2] % MOD) % MOD << '\n';
    }
    return 0;
}