Cod sursa(job #3365809)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 24 septembrie 2026 21:28:59
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.44 kb
#include <bits/stdc++.h>
using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
#define int long long
const int DIM = 3, MOD = 666013;

struct Matrix
{
    vector<vector<int>> mat = vector<vector<int>>(DIM, vector<int>(DIM, 0));
    Matrix operator * (const Matrix &other) const
    {
        Matrix prod;
        for(int i = 0; i < DIM; i++)
            for(int j = 0; j < DIM; j++)
                for(int k = 0; k < DIM; k++)
                    prod.mat[i][j] = (prod.mat[i][j] + mat[i][k] * other.mat[k][j]) % MOD;
        return prod;
    }
    Matrix power(int exp)
    {
        Matrix pwr, base = *this;
        for(int i = 0; i < DIM; i++)
            pwr.mat[i][i] = 1;
        while(exp != 0)
        {
            if(exp % 2 == 1)
                pwr = pwr * base;
            base = base * base;
            exp /= 2;
        }
        return pwr;
    }
};

void solve_testcase()
{
    int x, y, z, a, b, c, n;
    fin >> x >> y >> z >> a >> b >> c >> n;
    Matrix A;
    A.mat[0][0] = a, A.mat[0][1] = b, A.mat[0][2] = c;
    A.mat[1][0] = 1, A.mat[1][1] = 0, A.mat[1][2] = 0;
    A.mat[2][0] = 0, A.mat[2][1] = 1, A.mat[2][2] = 0;
    A = A.power(n - 2);
    fout << (z * A.mat[0][0] + y * A.mat[0][1] + x * A.mat[0][2]) % MOD << "\n";
}

signed main()
{
    int t;
    fin >> t;
    while(t--)
        solve_testcase();

    fin.close();
    fout.close();
    return 0;
}