Cod sursa(job #1017592)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 27 octombrie 2013 23:06:32
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <iostream>
#include <fstream>
#include <cstring>

using namespace std;

const int mod = 666013;

typedef unsigned Matrix[4][4];

int T, N, A, B, C, X, Y, Z;

inline void mult( Matrix A, Matrix B )
{
    Matrix aux;

    memset( aux, 0, sizeof ( aux ) );

    for ( int k = 1; k <= 3; ++k )
        for ( int i = 1; i <= 3; ++i )
            for ( int j = 1; j <= 3; ++j )
                aux[i][j] = ( aux[i][j] + A[i][k] * B[k][j] * 1LL ) % mod;

    memcpy( A, aux, sizeof ( aux ) );
}

int power()
{
    Matrix SOL, I;

    memset ( SOL, 0, sizeof ( SOL ) );
    memset ( I, 0, sizeof ( I ) );

    SOL[1][2] = SOL[2][3] = 1;
    SOL[3][1] = C; SOL[3][2] = B; SOL[3][3] = A;

    I[1][1] = 1;
    I[2][2] = 1;
    I[3][3] = 1;

    for ( int i = 0; ( 1 << i ) <= N; ++i )
    {
        if ( N & ( 1 << i ) )
        {
            mult( I, SOL );
        }

        mult( SOL, SOL );
    }

    return ( I[1][1] * X + I[1][2] * Y + I[1][3] * Z ) % mod;
}

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

    for ( f >> T; T; T-- )
    {
        f >> X >> Y >> Z >> A >> B >> C >> N;

        g << power() << "\n";
    }

    return 0;
}