Cod sursa(job #2980911)

Utilizator SerbanCaroleSerban Carole SerbanCarole Data 16 februarie 2023 21:59:44
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.38 kb
#include <fstream>
using namespace std;

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

const int MOD = 666013;

struct matrice{

    int m[3][3];

};

const matrice nula{

    {{1,0,0},
    {0,1,0},
    {0,0,1}}
};

matrice prod( matrice a , matrice b ){

    matrice aux;

    for(int i = 0 ; i < 3 ; i++){

        for(int j = 0 ; j < 3 ; j++){

            aux.m[i][j] = 0;

            for(int k = 0 ; k < 3 ; k++){

                aux.m[i][j] = (aux.m[i][j] + (a.m[i][k]*b.m[k][j])%MOD)%MOD;
            }
        }
    }

    return aux;
}

matrice fastpow( matrice x , int p){

    if(!p) return nula;

    if(p%2) return(prod(x,fastpow(x,p-1)));

    matrice aux = fastpow(x,p/2);

    return prod(aux,aux);

}

void testcase(){

    int x , y , z , a , b , c , n;

    cin >> x >> y >> z >> a >> b >> c >> n;

    matrice mat;

    int p = n-2;

    mat.m[0][0] = mat.m[1][0] = mat.m[1][1] = mat.m[0][2] = 0;

    mat.m[0][1] = mat.m[1][2] = 1;

    mat.m[2][0] = c;
    mat.m[2][1] = b;
    mat.m[2][2] = a;

    mat = fastpow(mat,p);

    int v[3] = {x,y,z};

    int rez = 0;

    for(int i = 0 ; i < 3 ; i++){

        rez = (rez + (mat.m[2][i]*v[i])%MOD)%MOD;
    }

    cout << rez << '\n';
}

int main(){

    int t; cin >> t;

    while(t--){

        testcase();
    }

    return 0;
}