Cod sursa(job #3321064)

Utilizator Andrei-Dani-10Pisla Andrei Daniel Andrei-Dani-10 Data 8 noiembrie 2025 10:09:04
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.2 kb
#include <fstream>

using namespace std;

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

const int nmax = 10, mod = 666013;

int add(int x, int y){
    x += y;
    if(x >= mod)
        x -= mod;
    return x;
}

struct matrix{
    int nn, mm, aa[nmax + 2][nmax + 2];

    matrix() : nn(0), mm(0) {};
    matrix(int a0, int a1){ nn = a0; mm = a1; }

    matrix operator * (const matrix &obj) const {
        matrix prod = matrix((*this).nn, obj.mm);

        for(int i = 1; i <= prod.nn; i++){
            for(int j = 1; j <= prod.mm; j++)
                prod.aa[i][j] = 0;
        }

        for(int i = 1; i <= prod.nn; i++){
            for(int j = 1; j <= prod.mm; j++){
                for(int k = 1; k <= (*this).mm; k++){
                    prod.aa[i][j] = add(prod.aa[i][j], 1ll * (*this).aa[i][k] * obj.aa[k][j] % mod);
                }
            }
        }

        return prod;
    }
};

/**
a[1] = x, a[2] = y, a[3] = z
a[k] = A * a[k - 1] + B * a[k - 2] + C * a[k - 3]

mat M * [a[3], a[2], a[1]] = [a[4], a[3], a[2]]

a[4] = a[3] * A + a[2] * B + a[1] * C
a[3] = a[3] * 1 + a[2] * 0 + a[1] * 0
a[2] = a[3] * 0 + a[2] * 1 + a[1] * 0
**/

matrix exp(matrix a, int b){

    matrix prod = matrix(a.nn, a.mm);

    for(int i = 1; i <= a.nn; i++){
        for(int j = 1; j <= a.mm; j++)
            prod.aa[i][j] = (i == j);
    }

    for(; b > 0; ){
        if(b & 1){
            prod = (prod * a);
        }

        a = (a * a);
        b >>= 1;
    }

    return prod;
}

int xx, yy, zz, aa, bb, cc, n;

void solve(){

    in>>xx>>yy>>zz>>aa>>bb>>cc>>n; n++;

    matrix base = matrix(3, 3);
    base.aa[1][1] = aa; base.aa[1][2] = bb; base.aa[1][3] = cc;
    base.aa[2][1] = 1;  base.aa[2][2] = 0;  base.aa[2][3] = 0;
    base.aa[3][1] = 0;  base.aa[3][2] = 1;  base.aa[3][3] = 0;

    base = exp(base, n - 3);

    matrix init = matrix(3, 1);

    init.aa[1][1] = zz;
    init.aa[2][1] = yy;
    init.aa[3][1] = xx;

    matrix output = (base * init);

    out<<output.aa[1][1]<<"\n";

    return;
}

int main(){

    int tests = 1; in>>tests;
    for(int i = 1; i <= tests; i++)
        solve();

    return 0;
}