Cod sursa(job #3203329)

Utilizator amcbnCiobanu Andrei Mihai amcbn Data 13 februarie 2024 15:17:59
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.52 kb
#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
const char nl = '\n';
const char sp = ' ';
const int inf = 0x3f3f3f3f;
const int mod = 666013;
const char out[2][4]{ "NO", "YES" };
#define all(a) a.begin(), a.end()
using ll = long long;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");

using matrix = vector<vector<ll>>;

matrix id{
    { 1, 0, 0 },
    { 0, 1, 0 },
    { 0, 0, 1 }
};

matrix multiply(const matrix& a, const matrix& b) {
    matrix c(a.size(), vector<ll>(b[0].size()));
    for (int i = 0; i < a.size(); ++i) {
        for (int j = 0; j < b[0].size(); ++j) {
            for (int k = 0; k < a[0].size(); ++k) {
                c[i][j] = (c[i][j] + a[i][k] * b[k][j] % mod) % mod;
            }
        }
    }
    return c;
}

matrix ppow(matrix n, ll p) {
    if (p == 0) {
        return id;
    }
    matrix rt = ppow(n, p >> 1);
    return multiply(multiply(rt, rt), p & 1 ? n : id);
}

void testcase() {
    ll x, y, z, a, b, c, n;
    cin >> x >> y >> z >> a >> b >> c >> n;
    matrix fib{
        { a, 1, 0 },
        { b, 0, 1 },
        { c, 0, 0 },
    };
    matrix init{
        { z, y, x }
    };
    fout << multiply(init, ppow(fib, n - 2))[0][0] << nl;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int tc;
    fin >> tc;
    for (int t = 1; t <= tc; ++t) {
#ifdef _DEBUG
        cerr << "=== Subtask " << t << " ===" << nl;
#endif
        testcase();
#ifdef _DEBUG
        cerr << nl;
#endif
    }
}