Cod sursa(job #2871699)

Utilizator the4Designerthe4Designer the4Designer Data 15 martie 2022 15:39:01
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.17 kb
#include <bits/stdc++.h>
using namespace std;

vector<vector<long unsigned>> multiply_matrix(vector<vector<long unsigned>> a, vector<vector<long unsigned>> b) {
    if (a.size() < 1 || a[0].size() != b.size()) {
        exit(-1);
    }

    vector<vector<long unsigned>> result(a.size(), vector<long unsigned>(b[0].size(), 0));

    // lungimea unei linii din prima matrice,
    // egala cu lungimea unei coloane din a doua matrice
    int vector_size = a[0].size();

    for (long unsigned i = 0; i < a.size(); ++i) {
        for (long unsigned j = 0; j < b[0].size(); ++j) {
            for (int k = 0; k < vector_size; ++k) {
                result[i][j] = 1LL * result[i][j] + 1LL * a[i][k] * b[k][j];
            }
        }
    }

    return result;
}

vector<vector<long unsigned>> fast_matrix_pow(vector<vector<long unsigned>> base_matrix, int exponent) {
    if (exponent == 0) {
        vector<vector<long unsigned>> result(base_matrix.size(), vector<long unsigned>(base_matrix.size(), 0));

        for (long unsigned i = 0; i < base_matrix.size(); ++i) {
            result[i][i] = 1;
        }

        return result;
    }

    vector<vector<long unsigned>> result = fast_matrix_pow(base_matrix, exponent / 2);

    if (exponent % 2 == 0) {
        return multiply_matrix(result, result);
    }

    return multiply_matrix(base_matrix, fast_matrix_pow(base_matrix, exponent - 1));
}

int main(void) {
    freopen("iepuri.in","r",stdin);
	freopen("iepuri.out","w",stdout);

    int t;
    cin >> t;

    for (int i = 0; i < t; ++i) {
        int x, y, z;
        cin >> x >> y >> z;

        int a, b, c, n;
        cin >> a >> b >> c >> n;

        // d day: a*(iepuri[d-1]) + b*(iepuri[d-2])
        //                        + c*(iepuri[d-3])

        // [a b c      *    [iepuri_(d-1)   =    [iepuri_d
        //  1 0 0            iepuri_(d-2)         iepuri_(d-1)
        //  0 1 0]           iepuri_(d-3)]        iepuri_(d-2)]

        int mod = 666013;

        vector<vector<long unsigned>> matrix = {{1LU * a, 1LU * b, 1LU * c}, {1, 0, 0}, {0, 1, 0}};
        cout << multiply_matrix(fast_matrix_pow(matrix, (n-2)), {{1LU * z}, {1LU * y}, {1LU * x}})[0][0] % mod << '\n';
    }

    return 0;
}