Cod sursa(job #2005341)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 26 iulie 2017 19:47:20
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <fstream>

using namespace std;

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

int T,x,y,z,a,b,c,i,j;
long long n,sol;
long long A[3][3],P[3][3],aux[3][3];

void produs(long long A[3][3], long long B[3][3], long long C[3][3], int t)
{
    for (int i=0; i<t; i++)
        for (int j=0; j<t; j++)
        {
            C[i][j] = 0;
            for (int l=0; l<t; l++)
            {
                C[i][j] += A[i][l]*B[l][j];
                C[i][j] %= 666013;
            }
        }
}

void atribuire(long long A[3][3], long long B[3][3], int t)
{
    for (int i=0; i<t; i++)
        for (int j=0; j<t; j++)
            A[i][j] = B[i][j];
}

int main()
{
    fin >> T;
    for (;T--;)
    {
        fin >> x >> y >> z >> a >> b >> c >> n;
        for (i=0; i<3; i++)
            for (j=0; j<3; j++)
            {
                A[i][j] = 0;
                if (i == j)
                    P[i][j] = 1;
                else
                    P[i][j] = 0;
            }
        A[1][0] = 1;
        A[2][1] = 1;
        A[0][0] = a;
        A[0][1] = b;
        A[0][2] = c;
        n -= 2;
        while (n >= 1)
        {
            if (n%2 == 1)
            {
                produs(A, P, aux, 3);
                atribuire(P, aux, 3);
            }
            produs(A, A, aux, 3);
            atribuire(A, aux, 3);
            n /= 2;
        }
        sol = P[0][0]*z;
        sol %= 666013;
        sol += P[0][1]*y;
        sol %= 666013;
        sol += P[0][2]*x;
        sol %= 666013;
        fout << sol << "\n";
    }
    return 0;
}