Cod sursa(job #3157878)

Utilizator PsyDuck1914Feraru Rares-Serban PsyDuck1914 Data 17 octombrie 2023 10:50:36
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.2 kb
#include <bits/stdc++.h>

using namespace std;

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

const int NMAX = 3;
const int MOD = 666013;

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

struct mat{
  int a[NMAX][NMAX];  
}nullMat, prodMat, initMat;

mat prod(mat x, mat y, int n, int m, int brh){
    
    mat aux = {{
        {0, 0, 0},
        {0, 0, 0},
        {0, 0, 0}
    }};
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            for(int k=0; k<brh; k++){
                aux.a[i][j] += (long long)x.a[i][k] * y.a[k][j] % MOD;
                aux.a[i][j] %= MOD;
            }
        }
    }
    
    return aux;
}

mat pwr(mat x, int n){
    if(n == 0)
        return nullMat;
    if(n % 2 == 0)
        return pwr(prod(x, x, NMAX, NMAX, NMAX), n/2);
    return prod(x, pwr(prod(x, x, NMAX, NMAX, NMAX), n/2), NMAX, NMAX, NMAX);
}

int main()
{
    int t;
    f >> t;
    for(int i=1; i<=t; i++){
        f >> x >> y >> z >> a >> b >> c >> n;
        
        nullMat = {{ {1, 0, 0}, {0, 1, 0}, {0, 0, 1} }};
        prodMat = {{ {0, 1, 0}, {0, 0, 1}, {c, b, a} }};
        initMat = {{ {x}, {y}, {z} }};
        
        g << prod(pwr(prodMat, n-2), initMat, 3, 1, 3).a[2][0] << "\n";
    }
    

    return 0;
}