Cod sursa(job #1735086)

Utilizator codebreaker24Tivadar Ionut codebreaker24 Data 28 iulie 2016 22:58:10
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.07 kb
#include <iostream>
#include <fstream>
using namespace std;

const int nmax = 3;
const int m = 666013;
struct Matrix
{
    long long V[nmax][nmax];
};

int x, y, z;
int a, b, c;
int t;
long long  n;

Matrix*  mulMatrix(Matrix *A, Matrix *B)
{
    Matrix *C  = new Matrix;
    long long sum;
    for(int i=0; i<nmax; i++)
    {
        for(int j=0; j<nmax;j++)
        {
            sum = 0;
            for(int r=0; r<nmax; r++)
                sum += (A->V[i][r]*(B->V[r][j]))%m;

             C->V[i][j] = sum %m;
        }
    }
    return C;
}

void cpyMatrix(Matrix *A, Matrix *B)
{

    for(int i=0; i<nmax; i++)
    {
        for(int j=0; j<nmax; j++)
        {
            A->V[i][j] = B->V[i][j];
        }
    }
}
void printMatrix(Matrix*A)
{

    for(int i=0; i<nmax; i++)
    {
        for(int j=0; j<nmax; j++)
            cout << A->V[i][j] << " ";
        cout <<  '\n';

    }
    cout <<  '\n';
}

void initMatrix(Matrix *A)
{
    for(int i=0; i<nmax; i++)
        for(int j=0; j<nmax; j++)
        A->V[i][j] = 0;
}

Matrix* expMatrix(Matrix *A, long long n)
{
   Matrix *I = new Matrix;
   initMatrix(I);

   for(int j=0; j<nmax; j++)
        I->V[j][j] = 1;
   if(n <= 0)
    return I;


   while ( n > 1)
 {
     if(n&1)
         I  = mulMatrix(A,I);
         n >>= 1;
        A  = mulMatrix(A,A);

 }
    return mulMatrix(A,I);

}


int main()

{

    ifstream fin ("iepuri.in");
    ofstream fout("iepuri.out");
    Matrix *A = new Matrix;
    Matrix *An = new Matrix;


        fin >> t;


    for(int j=0; j<t; j++)
        {

        initMatrix(A);

        fin >> x >> y >> z;
        fin >> a >> b >> c;
        fin >> n;
        A->V[0][0] = a % m;
        A->V[0][1] = b % m;
        A->V[0][2] = c % m;
        A->V[1][0] = 1;
        A->V[2][1] = 1;


            An = expMatrix(A,n-2);
            fout <<((An->V[0][2] *x )%m + (An->V[0][1] *y )%m + (An->V[0][0] *z )%m)%m << '\n';

        }






     fin.close();
     fout.close();

    return 0;
}