Cod sursa(job #3271158)

Utilizator popuPop Matei Tudor popu Data 25 ianuarie 2025 11:36:34
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.46 kb
#include <bits/stdc++.h>

#define ll long long

using namespace std;

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

const ll MOD = 666013;
const int nmax = 4;

struct Matrix
{
    int n, m;
    ll mt[nmax][nmax];

    Matrix(int n, int m)
    {
        this-> n = n;
        this-> m = m;
        for(int i = 1; i <= n; i++) {
            for(int j = 1; j <= m; j++)
                mt[i][j] = 0;
        }
    }
};

Matrix m(3, 3);

Matrix multiply(Matrix a, Matrix b)
{
    Matrix res(a.n, b.m);
    for(int i = 1; i <= a.n; i++) {
        for(int j = 1; j <= b.m; j++) {
            for(int k = 1; k <= a.m; k++)
                res.mt[i][j] += a.mt[i][k] * b.mt[k][j];
            res.mt[i][j] %= MOD;
        }
    }
    return res;
}

Matrix logpow(Matrix b, int exp)
{
    exp--;
    Matrix res = b;
    while(exp) {
        if(exp & 1)
            res = multiply(res, b);
        exp >>= 1;
        b = multiply(b, b);
    }
    return res;
}

void solve()
{
    ll x, y, z, a, b, c, n;
    fin >> x >> y >> z >> a >> b >> c >> n;
    m.mt[1][3] = c;
    m.mt[2][3] = b;
    m.mt[3][3] = a;
    Matrix aux = logpow(m, n);
    Matrix res(1, 3);
    res.mt[1][1] = x;
    res.mt[1][2] = y;
    res.mt[1][3] = z;
    res = multiply(res, aux);
    fout << res.mt[1][1] << '\n';
}

int main()
{
    m.mt[2][1] = m.mt[3][2] = 1;

    int t;
    fin >> t;
    while(t--)
        solve();

    return 0;
}