Cod sursa(job #3345214)

Utilizator NeamtuMateiNeamtu Matei-Constantin NeamtuMatei Data 8 martie 2026 14:59:34
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

#define in fin
#define out fout
#define int long long
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");

const int MAXN = 2e9; // what
const int MOD = 666013;

struct matrice {
    int mat[3][3]; // M3

    matrice() { memset(mat, 0, sizeof(mat)); }

    matrice(initializer_list<initializer_list<int>> init) {
        int i = 0;
        for (auto row: init) {
            int j = 0;
            for (auto col: row)
                mat[i][j++] = col;
            i++;
        }
    }

    matrice operator * (matrice b) {
        matrice rez;
        for (int i = 0; i < 3; i++)
            for (int j = 0; j < 3; j++)
                for (int k = 0; k < 3; k++)
                    rez.mat[i][j] = (rez.mat[i][j] + mat[i][k] * b.mat[k][j]) % MOD;
        return rez;
    }

    void afis() {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++)
                out << mat[i][j] << ' ';
            out << '\n';
        }
        out << '\n';
    }
};

matrice fast_expo(matrice a, int b) {
    matrice rez = {
        {1, 0, 0},
        {0, 1, 0},
        {0, 0, 1}
    };

    while (b) {
        if (b & 1) rez = rez * a;
        a = a * a;
        b >>= 1;
    }

    return rez;
}

int teste;
int x, y, z, a, b, c, n;

signed main() {
    in >> teste;

    for (int _ = 0; _ < teste; _++) {

        in >> x >> y >> z >> a >> b >> c >> n;

        if (n == 0) out << x;
        else if (n == 1) out << y;
        else if (n == 2) out << z;
        else {
            matrice tranzitie = {
                {0, 0, c},
                {1, 0, b},
                {0, 1, a}
            };

            matrice rez = fast_expo(tranzitie, n - 2);

            int V[] = {x, y, z};
            int rezultat = (rez.mat[0][2] * x % MOD +
                           rez.mat[1][2] * y % MOD +
                           rez.mat[2][2] * z % MOD) % MOD;

            out << rezultat << '\n';
        }
    }

    return 0;
}