Cod sursa(job #3158990)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 20 octombrie 2023 14:01:15
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda HLO 2023 - Cls 11-12 - Tema 0 Marime 1.73 kb
#include <bits/stdc++.h>

using i64 = long long;


const int MOD = 666013;

int mul(const int &a, const int &b) {
    return 1LL * a * b % MOD;
}

int add(int a, const int &b) {
    a += b;
    if (a >= MOD) {
        a -= MOD;
    }
    return a;
}

using Matrix = std::vector<std::vector<int>>;

void debug(Matrix A) {
    for (auto x : A) {
        for (auto y : x) {
            std::cout << y << ' ';
        }
        std::cout << "\n";
    }
    std::cout << "\n";
}

Matrix I = {
    {1, 0, 0},
    {0, 1, 0},
    {0, 0, 1}
};

Matrix O = {
    {0, 0, 0},
    {0, 0, 0},
    {0, 0, 0}
};

Matrix matrix_mul(Matrix A, Matrix B) {
    int n = (int)A.size();
    int m = (int)B[0].size();
    Matrix res = O;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            for (int k = 0; k < n; ++k) {
                res[i][j] = add(res[i][j], mul(A[i][k], B[k][j]));
            }
        }
    }
    return res;
}

Matrix binpow(Matrix B, int e) {
    Matrix res = I;
    while (e > 0) {
        if (e & 1) {
            res = matrix_mul(res, B);
        }
        B = matrix_mul(B, B);
        e /= 2;
    }
    return res;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);

    freopen("iepuri.in", "r", stdin);
    freopen("iepuri.out", "w", stdout);

    int tt = 1;
    std::cin >> tt;
    while (tt--) {
        int x, y, z, a, b, c, n;
        std::cin >> x >> y >> z >> a >> b >> c >> n;

        Matrix base = {
            {0, 1, 0},
            {0, 0, 1},
            {c, b, a}
        };
        Matrix F = {{x}, {y}, {z}};
        // debug(matrix_mul(base, base));
        Matrix ans = binpow(base, n - 2);
        ans = matrix_mul(ans, F);
        printf("%d\n", ans[2][0]);
    }
    return 0;
}