Cod sursa(job #2640821)

Utilizator AndreiPaval03Andrei Paval AndreiPaval03 Data 8 august 2020 14:22:23
Problema Iepuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.25 kb
#include <bits/stdc++.h>

using namespace std;

#define MOD 666013

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

void matProduct(int A[4][4], int B[4][4], int C[4][4])
{
	int rez[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};

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

	for (int i = 1; i <= 3; ++i)
		for (int j = 1; j <= 3; ++j)
			C[i][j] = rez[i][j];
}

void matExponentiation(int n, int mat[4][4])
{
	int auxMat[4][4] = {{0, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 1}};
    	
	while (n > 1)
	{
		if (n % 2)
			matProduct(mat, auxMat, auxMat);
		matProduct(mat, mat, mat);
		n >>= 1;
	}

	matProduct(mat, auxMat, mat);
}

int main()
{
    int n;
    int mat[4][4]; 
    int x, y, z;
    int numTests;

    cin >> numTests;
    while (numTests--)
    {
    	fin >> x >> y >> z >> mat[1][1] >> mat[1][2] >> mat[1][3] >> n;
    	mat[2][1] = mat[3][2] = 1;
    	mat[2][2] = mat[2][3] = mat[3][1] = mat[3][3] = 0;

    	matExponentiation(n - 2, mat);

    	fout << ((1LL) * mat[1][1] * z + (1LL) * mat[1][2] * y + (1LL) * mat[1][3] * x) % MOD << '\n';
    }

	return 0;
}