Cod sursa(job #3266323)

Utilizator Barbu_MateiBarbu Matei Barbu_Matei Data 7 ianuarie 2025 14:38:24
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <bits/stdc++.h>
using namespace std;

const int MOD = 666013;
int t, x, y, z, a, b, c, n;
long long mt[2][4], w[4][4];

void reset() {
    memset(w, 0, sizeof(w));
    mt[1][1] = x;
    mt[1][2] = y;
    mt[1][3] = z;
    w[2][1] = w[3][2] = 1;
    w[1][3] = c;
    w[2][3] = b;
    w[3][3] = a;
}

void prod(int limit, long long a[4][4], long long b[4][4]) {
    long long res[4][4];
    memset(res, 0, sizeof(res));
    for (int i = 1; i <= limit; ++i) {
        for (int j = 1; j <= 3; ++j) {
            for (int k = 1; k <= 3; ++k) {
                res[i][j] = (res[i][j] + (a[i][k] * b[k][j])) % MOD;
            }
        }
    }
    for (int i = 1; i <= limit; ++i) {
        for (int j = 1; j <= 3; ++j) {
            a[i][j] = res[i][j];
        }
    }
}

void exp(long long w[4][4], int pwr) {
    long long res[4][4];
    memset(res, 0, sizeof(res));
    res[1][1] = res[2][2] = res[3][3] = 1;
    while (pwr != 0) {
        if (pwr & 1 == 1) {
            prod(3, res, w);
        }
        prod(3, w, w);
        pwr >>= 1;
    }
    for (int i = 1; i <= 3; ++i) {
        for (int j = 1; j <= 3; ++j) {
            w[i][j] = res[i][j];
        }
    }
}

int main() {
    ifstream cin("iepuri.in");
    ofstream cout("iepuri.out");
    cin >> t;
    while (t--) {
        cin >> x >> y >> z >> a >> b >> c >> n;
        reset();
        exp(w, n - 1 - 1);
        prod(1, mt, w);
        cout << mt[1][3] << "\n";
    }
}