Cod sursa(job #3344092)

Utilizator Roberto_CChirvasitu Roberto Roberto_C Data 1 martie 2026 13:15:40
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long

int x,y,z,a,b,c,n,t;
const int MOD = 666013;

struct matrix{
    int mat[3][3];

    void init(){
        for(int i = 0; i < 3; i++)
            for(int j = 0; j < 3; j++)
                this->mat[i][j] = 0;
    }
};

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

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

void read(){
    cin >> x >> y >> z >> a >> b >> c >> n;
}

void solve(){
    matrix t;
    t.init();
    t.mat[0][0] = a;
    t.mat[0][1] = b;
    t.mat[0][2] = c;
    t.mat[1][0] = 1;
    t.mat[2][1] = 1;
    matrix rasp = log_pow(t,n-2);
    cout << 1LL*((rasp.mat[0][0] * z) % MOD + (rasp.mat[0][1] * y) % MOD + (rasp.mat[0][2] * x) % MOD) << '\n';
}

int main() {
    freopen("iepuri.in","r",stdin);
    freopen("iepuri.out","w",stdout);
    cin >> t;
    while(t--){
        read();
        solve();
    }
    return 0;
}