Cod sursa(job #2983535)

Utilizator CosmincreatoMarasoiu Cosmin Cosmincreato Data 22 februarie 2023 16:34:06
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <cstring>

using namespace std;

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

const int MOD = 666013;

int A[3][3];
int I[3][3];
int x, y, z, a, b, c, n;

void init()
{
    A[0][2] = A[1][1] = A[2][1] = A[2][2] = 0;
    A[0][1] = A[1][2] = 1;
    A[0][0] = a;
    A[1][0] = b;
    A[2][0] = c;
    //
    for(int i = 0; i < 3; ++i)
    {
        for(int j = i + 1; j < 3; ++j)
            I[i][j] = I[j][i] = 0;
        I[i][i] = 1;
    }
}

void multmat(int a[][3], int b[][3])
{
    int c[3][3];
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
        {
            c[i][j] = 0;
            for(int k = 0; k < 3; k++)
                c[i][j] = (c[i][j] + 1LL * a[i][k] * b[k][j]) % MOD;
        }
    memcpy(a, c, sizeof(c));
}

void puteremat(int p)
{
    while(p > 0)
        if(p % 2 == 0)
        {
            multmat(A, A);
            p /= 2;
        }
        else
        {
            multmat(I, A);
            p--;
        }
}

int main()
{
    int k;
    fin >> k;
    while(k--)
    {
        fin >> x >> y >> z >> a >> b >> c >> n;
        init();
        puteremat(n - 2);
        fout << (1LL * z * I[0][0] + 1LL * y * I[1][0] + 1LL * x * I[2][0]) % MOD << '\n';
    }
    fin.close();
    fout.close();
    return 0;
}