Cod sursa(job #3295417)

Utilizator _andr31Rusanescu Andrei-Marian _andr31 Data 5 mai 2025 17:08:07
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <bits/stdc++.h>

using namespace std;

#define KMAX 3
#define MOD 666013
void multiply_matrix(long long A[KMAX][KMAX], long long B[KMAX][KMAX], long long C[KMAX][KMAX]) {
    long long tmp[KMAX][KMAX];

    for (int i = 0; i < KMAX; ++i) {
        for (int j = 0; j < KMAX; ++j) {
            long sum = 0;
            for (int k = 0; k < KMAX; ++k) {
                sum = (sum + 1LL * A[i][k] * B[k][j]) % MOD;
            }
            tmp[i][j] = sum;
        }
    }

    memcpy(C, tmp, sizeof(tmp));
}

void power_matrix(long long C[KMAX][KMAX], long long exp, long long res[KMAX][KMAX]) {
    long long tmp[KMAX][KMAX];
    for (int i = 0; i < KMAX; ++i)
        for (int j = 0; j < KMAX; ++j)
            tmp[i][j] = i == j ? 1 : 0;
    
    while (exp > 1) {
        if (exp & 1) {
            --exp;
            multiply_matrix(tmp, C, tmp);
        } else {
            multiply_matrix(C, C, C);
            exp /= 2;
        }
    }
    multiply_matrix(C, tmp, res);
}

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

    int t;
    fin >> t;

    while (t--) {
        int x, y, z, a, b, c, n;
        fin >> x >> y >> z >> a >> b >> c >> n;

        long long res[KMAX][KMAX] = {{0, 0, c}, {1, 0, b}, {0, 1, a}};
        power_matrix(res, n - 2, res);
        long long ans = (x * res[0][2] + y * res[1][2] + z * res[2][2]) % MOD;
        fout << ans << '\n';
    }


    return 0;
}