Cod sursa(job #3143285)

Utilizator BuzdiBuzdugan Rares Andrei Buzdi Data 28 iulie 2023 19:14:04
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.28 kb
#include <fstream>

using namespace std;

ifstream cin("iepuri.in");
ofstream cout("iepuri.out");

const int MOD = 666013;

int T;

void Product(int a[3][3], int b[3][3])
{
    int rez[3][3] = {0};
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            for(int k = 0; k < 3; k++)
                rez[i][j] = ((long long)rez[i][j] + ((long long)a[i][k] * b[k][j]) % MOD) % MOD;
    
    for(int i = 0; i < 3; i++)
        for(int j = 0; j < 3; j++)
            a[i][j] = rez[i][j];
}

void Power(int rez[3][3], int a[3][3], int b)
{
    while(b)
    {
        if(b % 2 == 1)
            Product(rez, a);
        Product(a, a);
        b /= 2;
    }
}

int main()
{
    cin >> T;
    while(T--)
    {
        int X, Y, Z, A, B, C, N;
        cin >> X >> Y >> Z >> A >> B >> C >> N;

        int M[3][3] = {
            {X, Y, Z},
            {0, 0, 0},
            {0, 0, 0},
        };

        int z[3][3] = {
            {0, 0, C},
            {1, 0, B},
            {0, 1, A},
        };

        int rez[3][3] = {
            {1, 0, 0},
            {0, 1, 0},
            {0, 0, 1},
        };

        Power(rez, z, N - 2);
        Product(M, rez);

        cout << M[0][2] << '\n';
    }




    return 0;
}