Cod sursa(job #2059112)

Utilizator BrandonChris Luntraru Brandon Data 6 noiembrie 2017 17:45:05
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>
#include <cassert>

using namespace std;

const int Mod = 666013;

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

class MatrixType {
public:
	long long mat[3][3];

	MatrixType(long long a11 = 1, long long a12 = 0, long long a13 = 0,
						 long long a21 = 0, long long a22 = 1, long long a23 = 0,
						 long long a31 = 0, long long a32 = 0, long long a33 = 1) {

		mat[0][0] = a11;
		mat[0][1] = a12;
		mat[0][2] = a13;
		mat[1][0] = a21;
		mat[1][1] = a22;
		mat[1][2] = a23;
		mat[2][0] = a31;
		mat[2][1] = a32;
		mat[2][2] = a33;
	}

	MatrixType operator * (const MatrixType &other) const {
		MatrixType ans(0, 0, 0,
									 0, 0, 0,
									 0, 0, 0);

		for (int i = 0; i < 3; ++i) {
			for (int j = 0; j < 3; ++j) {
				for (int k = 0; k < 3; ++k) {
					ans.mat[i][j] += mat[i][k] * other.mat[k][j];
					ans.mat[i][j] %= Mod;
				}
			}
		}

		return ans;
	}
};

MatrixType LogPow(MatrixType base, int exp) {
	if (exp == 0) {
		return MatrixType();
	}

	if (exp % 2 == 0) {
		MatrixType sqRoot = LogPow(base, exp / 2);
		return sqRoot * sqRoot;
	}

	return LogPow(base, exp - 1) * base;
}

int main() {
	int T;
	cin >> T;
	for (int currTask = 1; currTask <= T; ++currTask) {
		int x, y, z, a, b, c, n;
		cin >> x >> y >> z >> a >> b >> c >> n;
		MatrixType M(0, 0, c,
								 1, 0, b,
								 0, 1, a);

		MatrixType S(x, y, z,
								 0, 0, 0,
								 0, 0, 0);

		MatrixType Ans = S * LogPow(M, n);		
		cout << Ans.mat[0][0] << '\n';
	}
	return 0;
}