Cod sursa(job #3379252)

Utilizator TimofeiFilipTimofei Filip Emanuel TimofeiFilip Data 29 noiembrie 2026 21:41:11
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.83 kb
#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
const int MOD = 666013;

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

class Matrix {
public:
    int cols, rows;
    vector<vector<ll>> mat;

    Matrix(int rows, int colums) {
        this->cols = colums;
        this->rows = rows;
        mat.assign(rows, vector<ll>(cols, 0));
    }
    Matrix get_neutral_matrix() const {
        assert(rows == cols);

        Matrix result(rows, cols);

        for (int i = 0; i < rows; i++)
            result.mat[i][i] = 1;

        return result;
    }
    Matrix operator*(Matrix other) const {
        assert(cols == other.rows);
        Matrix result(rows, other.cols);

        for (int i = 0; i < rows; i++) {
            for (int k = 0; k < cols; k++) {
                for (int j = 0; j < other.cols; j++) {
                    result.mat[i][j] = (result.mat[i][j] + (mat[i][k] * other.mat[k][j]) % MOD) % MOD;
                }
            }
        }
        return result;
    }
    Matrix operator^(ll exp) const{
        assert(rows == cols);
        assert(exp >= 0);

        Matrix base = *this;
        Matrix result = get_neutral_matrix();


        while (exp > 0) {
            if (exp & 1)
                result = result * base;
            base = base * base;
            exp >>= 1;
        }
        return result;
    }
};

void solve_case() {
    ll X, Y, Z, A, B, C, N;
    fin >> X >> Y >> Z >> A >> B >> C >> N;
    Matrix start(1, 3);
    start.mat[0] = {X, Y, Z};

    Matrix next_matrix(3, 3);
    next_matrix.mat[0] = {0ll, 0ll, C};
    next_matrix.mat[1] = {1ll, 0ll, B};
    next_matrix.mat[2]  = {0ll, 1ll, A};

    Matrix result = start * (next_matrix ^ (N - 2ll));
    fout << result.mat[0][2] << '\n';
}

int main() {
    int t;
    for (fin >> t; t > 0; t--) {
        solve_case();
    }
    return 0;
}