Cod sursa(job #3158226)

Utilizator Andrei08Petcu Andrei Vlad Andrei08 Data 18 octombrie 2023 01:23:40
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.75 kb
#include <bits/stdc++.h>
#define MOD 666013

using namespace std;

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

long long nullMat[3][3] = {
    {1, 0, 0},
    {0, 1, 0},
    {0, 0, 1}
};

void clear(){
    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++)
            nullMat[i][j] = 0;
    nullMat[0][0] = nullMat[1][1] = nullMat[2][2] = 1;
}

void prod(long long a[3][3], long long b[3][3], int n, int k, int m){

    long long c[3][3] = {
      {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 y=0; y<k; y++){
                c[i][j] += (long long)a[i][y] * b[y][j] % MOD;
                c[i][j] %= MOD;
            }
        }
    }

    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            a[i][j] = c[i][j];

}

void pwr(long long a[3][3], int n){
    while(n){
        if(n % 2 == 0){
            n /= 2;
            prod(a, a, 3, 3, 3);
           // prod(nullMat, a, 3, 3, 3);
        }else{
            prod(nullMat, a, 3, 3, 3);
            n --;
        }
    }
}

int main()
{
    long long t, x, y, z, a, b, c, n;

    fin >> t;
    for(int i=1; i<=t; i++){

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

        long long initMat[3][1] = {
            {x},
            {y},
            {z}
        };

        long long mat[3][3] = {
            {0, 1, 0},
            {0, 0, 1},
            {c, b, a}
        };

        clear();

        pwr(mat, n-2);

        fout << (long long)((long long)(initMat[0][0] * nullMat[2][0]) % MOD + (long long)(initMat[0][1] * nullMat[2][1]) % MOD + (long long)(initMat[0][2] * nullMat[2][2]) %MOD) % MOD << "\n";

    }

    return 0;
}