Cod sursa(job #3169808)

Utilizator zetef3Dediu Stefan zetef3 Data 16 noiembrie 2023 01:12:48
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.29 kb
#include <fstream>
#include <iostream>
#include <vector>

#define MOD 666013

using namespace std;

ifstream f("iepuri.in");
ofstream g("iepuri.out");

int T;
vector<int> I;

struct mat {
    int l,c;
    int v[3][3];

    mat() {
        for (int i=0;i<3;i++)
            for (int j=0;j<3;j++)
                v[i][j]=0;
    }
};

mat mat_mult(mat A, mat B) {
    mat C;
    C.l=A.l;
    C.c=B.c;

    for (int i=0;i<C.l;i++)
        for (int j=0;j<C.c;j++)
            for (int k=0;k<A.c;k++)
                C.v[i][j]=(C.v[i][j]+(A.v[i][k]*B.v[k][j])%MOD)%MOD;

    return C;
}

mat mat_exp(mat M, int N) {
    mat P; P.l=3; P.c=3;
    P.v[0][0]=1;
    P.v[1][1]=1;
    P.v[2][2]=1;

    while (N) {
        if (N%2==1) {
            P=mat_mult(P,M);
        }
        M=mat_mult(M,M);
        N/=2;
    }
    return P;
}

void solve() {
    int X,Y,Z,A,B,C,N;

    f >> X >> Y >> Z >> A >> B >> C >> N;

    I.resize(N+1);
    I[0]=X;
    I[1]=Y;
    I[2]=Z;

    mat M;
    M.l=M.c=3;
    M.v[0][1]=1; M.v[1][2]=1;
    M.v[2][0]=C; M.v[2][1]=B; M.v[2][2]=A;

    mat ans=mat_exp(M,N);

    mat Im;
    Im.l=3; Im.c=1;
    Im.v[0][0]=I[0];
    Im.v[1][0]=I[1];
    Im.v[2][0]=I[2];

    mat rez=mat_mult(ans, Im);

    g << rez.v[0][0] << '\n';
}

int main()
{
    f >> T;
    while (T--)
        solve();
    return 0;
}