Cod sursa(job #2079192)

Utilizator iordache.bogdanIordache Ioan-Bogdan iordache.bogdan Data 30 noiembrie 2017 18:47:05
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>
using namespace std;

namespace iepuri {
    constexpr int MOD = 666013;
    typedef vector< vector<int> > matrix;

    matrix operator* (const matrix& lhs, const matrix& rhs) {
        matrix res(lhs.size(), vector<int>(rhs[0].size()));

        for (size_t i = 0; i < lhs.size(); ++i)
            for (size_t j = 0; j < rhs[0].size(); ++j)
                for (size_t k = 0; k < lhs[0].size(); ++k)
                    res[i][j] = int((res[i][j] + 1LL * lhs[i][k] * rhs[k][j]) % MOD);

        return res;
    }

    matrix raise_to_power(matrix base, int exponent) {
        matrix res({{1, 0, 0}, {0, 1, 0}, {0, 0, 1}});
        while (exponent > 0) {
            if (exponent & 1)
                res = res * base;
            exponent >>= 1;
            base = base * base;
        }

        return res;
    }
}

void solve_test() {
    int x, y, z;
    cin >> x >> y >> z;
    int a, b, c, n;
    cin >> a >> b >> c >> n;
    n -= 2;

    iepuri::matrix base = {{a, b, c}, {1, 0, 0}, {0, 1, 0}};
    iepuri::matrix final_matrix = iepuri::raise_to_power(base, n);

    cout << int(1LL * final_matrix[0][0] * z % iepuri::MOD
             + 1LL * final_matrix[0][1] * y % iepuri::MOD
             + 1LL * final_matrix[0][2] * x % iepuri::MOD) % iepuri::MOD;
    cout << endl;
}

void solve() {
    size_t test_count;
    cin >> test_count;

    while (test_count--)
        solve_test();
}

int main() {
    assert(freopen("iepuri.in", "r", stdin));
    assert(freopen("iepuri.out", "w", stdout));
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    solve();

    return 0;
}