Cod sursa(job #2276520)

Utilizator tigeraOprea Tereza Emilia tigera Data 4 noiembrie 2018 20:12:18
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.55 kb
//
//  main.cpp
//  iepuri
//
//  Created by Tereza Oprea on 04/11/2018.
//  Copyright © 2018 Tereza Oprea. All rights reserved.
//

#include <iostream>
#include <fstream>
#include <cstring>
#define M 666013

using namespace std;

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

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

struct matrix
{
    int m[3][3];
    matrix( )
    {
        memset (m, 0, sizeof(m));
    }
    matrix operator * (matrix b)
    {
        matrix out = matrix( );
        for (int i=0; i<3; i++)
            for (int j=0; j<3; j++)
                for (int k=0; k<3; k++)
                {
                    out.m[i][j] = (out.m[i][j] + 1LL * m[ i ][ k ] * b.m[ k ][ j ])%M;
                }
        return out;
    }
};

matrix A, B;
matrix unit = matrix( );

matrix mPow (matrix x, int n)
{
    if (n==0)
        return unit;
    matrix half = mPow (x, n/2);
    half = half * half;
    if (n%2)
        half = half * x;
    return half;
    
}

int main() {
    
    unit.m[0][0] = unit.m[1][1] = unit.m[2][2]  = 1;
    
    fin >> t;
    for (int i=1; i<=t; i++){
        A = matrix( );
        B = matrix( );
        fin >> x >> y >> z;
        fin >> a >> b >> c;
        fin >> n;
        
        B.m[0][0] = 0; B.m[0][1] = 0; B.m[0][2] = c;
        B.m[1][0] = 1; B.m[1][1] = 0; B.m[1][2] = b;
        B.m[2][0] = 0; B.m[2][1] = 1; B.m[2][2] = a;
        
        A.m[0][0] = x; A.m[0][1] = y; A.m[0][2] = z;
        
        B = mPow(B, n-2);
        
        A = A * B;
        fout << A.m[0][2] << '\n';
        
        
    }
    
    return 0;
}