Cod sursa(job #3330628)

Utilizator Roberto_CChirvasitu Roberto Roberto_C Data 20 decembrie 2025 16:19:59
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include<iostream>
#include<fstream>

using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
const int MOD = 666013;
int t;

struct matrice
{
    int mat[3][3];
    void init()
    {
        for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
                mat[i][j] = 0;
    }
};

matrice multiply(matrice a, matrice b)
{
    matrice ans;
    ans.init();
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            for(int k = 0; k < 3; k++)
            {
                ans.mat[i][j] = (ans.mat[i][j] + a.mat[i][k] * b.mat[k][j]) % MOD;
            }
        }
    }
    return ans;
}

matrice log_pow(matrice a, long long put)
{
    matrice iden;
    iden.init();
    for(int i = 0; i < 3; i++)
        iden.mat[i][i] = 1;
    while(put)
    {
        if(put & 1)
            iden = multiply(iden,a);
        a = multiply(a,a);
        put >>= 1;
    }
    return iden;
}

void solve()
{
    int n,x,y,z,a,b,c;
    fin >> x >> y >> z >> a >> b >> c >> n;
    matrice T;
    T.init();
    T.mat[0][0] = a;
    T.mat[1][0] = 1;
    T.mat[0][1] = b;
    T.mat[2][1] = 1;
    T.mat[0][2] = c;
    matrice ans;
    ans.init();
    ans = log_pow(T,n-2);///stim pentru 0,1,2
    long long rasp = 0;
    rasp = (rasp + ans.mat[0][0] * z) % MOD;
    rasp = (rasp + ans.mat[0][1] * y) % MOD;
    rasp = (rasp + ans.mat[0][2] * x) % MOD;
    fout << rasp << '\n';
}

int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(nullptr);
    fin >> t;
    while(t--)
        solve();
    return 0;
}