Cod sursa(job #3241726)

Utilizator Cristian_NegoitaCristian Negoita Cristian_Negoita Data 3 septembrie 2024 11:24:42
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.27 kb
#include <bits/stdc++.h>

using namespace std;

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

const int MOD = 666.013;

struct matrice
{
    long long v[3][3];
};

matrice multiplication(matrice a, matrice b)
{
    matrice rez;
    for (int row = 0; row < 3; row++)
    {
        for (int column = 0; column < 3; column++)
        {
            rez.v[row][column] = 0;
            for (int i = 0; i < 3; i++)
                rez.v[row][column] += a.v[row][i] * b.v[i][column];
            rez.v[row][column] %= MOD;
        }
    }
    return rez;
}

matrice putere(matrice a, int n)
{
    matrice rez = {{{1, 0, 0},
                    {0, 1, 0},
                    {0, 0, 1}}};

    while (n != 0)
    {
        if (n % 2 == 1)
            rez = multiplication(rez, a);
        a = multiplication(a, a);
        n /= 2;
    }
    return rez;
}

void solve()
{
    int x, y, z, a, b, c, n;
    fin >> x >> y >> z >> a >> b >> c >> n;

    matrice m = {{{a, b, c},
                  {1, 0, 0},
                  {0, 1, 0}}};
    matrice p = putere(m, n - 2);
    fout << (p.v[0][0] * z + p.v[0][1] * y + p.v[0][2] * x) % MOD << "\n";
}

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

    return 0;
}