Cod sursa(job #3371912)

Utilizator Andrei1209Andrei Mircea Andrei1209 Data 1 noiembrie 2026 18:43:35
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.68 kb
#include <fstream>

using namespace std;
ifstream fin("iepuri.in");
ofstream fout("iepuri.out");
const int MOD = 666013;
const int Nmax = 5;
void inmulteste( int a[Nmax][Nmax], int b[Nmax][Nmax], int n, int m, int p, int result[Nmax][Nmax] )
{
    int i, j, k;
    for ( i = 1; i <= n; ++i )
        for ( j = 1; j <= p; ++j )
        {
            result[i][j] = 0;
            for ( k = 1; k <= m; ++k )
                result[i][j] = ( result[i][j] + 1LL * a[i][k] * b[k][j] % MOD ) % MOD;
        }
}
void exp(int a[Nmax][Nmax], int n, int putere, int result[Nmax][Nmax])
{
    int i, j;
    if ( putere == 0 )
    {
        for ( i = 1; i <= n; ++i )
            for ( j = 1; j <= n; ++j )
                result[i][j] = (i == j );
    }
    else
    {
        if ( putere % 2 == 0 )
        {
            int aux[Nmax][Nmax];
            exp( a, n, putere / 2, aux );
            inmulteste(aux, aux, n, n, n, result );
        }
        else
        {
            int aux[Nmax][Nmax];
            exp(a, n, putere -1, aux);
            inmulteste(aux, a, n, n, n, result);
        }
    }
}
int main()
{
    int t;
    fin >> t;
    while (t-- )
    {
        int x, y, z, a, b, c, k;
        fin >> x >> y >> z >> a >> b >> c >> k;
        int A[Nmax][Nmax];
        A[1][1] = 0, A[1][2] = 0, A[1][3] = c;
        A[2][1] = 1, A[2][2] = 0, A[2][3] = b;
        A[3][1] = 0, A[3][2] = 1, A[3][3] = a;
        int B[Nmax][Nmax], result[Nmax][Nmax], aux[Nmax][Nmax];
        B[1][1] = x, B[1][2] = y, B[1][3] = z;

        exp( A, 3, k, aux );
        inmulteste(B, aux, 1, 3, 3, result);
        fout << result[1][1] << '\n';
    }
    return 0;
}