Cod sursa(job #2407072)

Utilizator MicuMicuda Andrei Micu Data 16 aprilie 2019 14:12:18
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.79 kb
#include <iostream>
#include <fstream>

using namespace std;

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

const int MOD=666013;

int A[3][3]={{0, 0, 0},
             {1, 0, 0},
             {0, 1, 0}};

int CA[3][3]={{0, 0, 0},
              {1, 0, 0},
              {0, 1, 0}};

void ProdMat(int A[3][3], int B[3][3], int R[3][3]);
void PutMat(int A[3][3], int n);

int main()
{
    int t;
    in >> t;
    for(int i=1; i<=t; i++)
    {
        int x, y, z, a, b, c, n;
        in >> x >> y >> z >> a >> b >> c >> n;
        for(int i=0; i<3; i++)
            for(int j=0; j<3; j++) A[i][j]=0;
        A[1][0]=1; A[2][1]=1;
        A[0][2]=CA[0][2]=c;
        A[1][2]=CA[1][2]=b;
        A[2][2]=CA[2][2]=a;
        PutMat(A, n-2);
        out << (x*A[0][2]%MOD+y*A[1][2]%MOD+z*A[2][2]%MOD)%MOD << '\n';
    }
    return 0;
}

void ProdMat(int A[3][3], int B[3][3], int R[3][3])
{
    int aux[3][3]={0};
    for(int i=0; i<3; i++) aux[0][0]+=A[0][i]*B[i][0]%MOD;
    for(int i=0; i<3; i++) aux[0][1]+=A[0][i]*B[i][1]%MOD;
    for(int i=0; i<3; i++) aux[0][2]+=A[0][i]*B[i][2]%MOD;
    for(int i=0; i<3; i++) aux[1][0]+=A[1][i]*B[i][0]%MOD;
    for(int i=0; i<3; i++) aux[1][1]+=A[1][i]*B[i][1]%MOD;
    for(int i=0; i<3; i++) aux[1][2]+=A[1][i]*B[i][2]%MOD;
    for(int i=0; i<3; i++) aux[2][0]+=A[2][i]*B[i][0]%MOD;
    for(int i=0; i<3; i++) aux[2][1]+=A[2][i]*B[i][1]%MOD;
    for(int i=0; i<3; i++) aux[2][2]+=A[2][i]*B[i][2]%MOD;

    for(int i=0; i<3; i++)
        for(int j=0; j<3; j++) R[i][j]=aux[i][j]%MOD;
}
void PutMat(int A[3][3], int n)
{
    if(n==1) return;
    if(n%2==1)
    {
        PutMat(A, n/2);
        ProdMat(A, A, A);
        ProdMat(CA, A, A);
    }
    else
    {
        PutMat(A, n/2);
        ProdMat(A, A, A);
    }
}