Cod sursa(job #3350049)

Utilizator DobraVictorDobra Victor Ioan DobraVictor Data 5 aprilie 2026 00:19:50
Problema Iepuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <iostream>
#include <fstream>
#include <stdint.h>

const int64_t MOD = 666013;

void MultMats(int64_t mat1[3][3], const int64_t mat2[3][3]) {
	int64_t res[3][3];
	for(int64_t i = 0; i != 3; ++i) {
		for(int64_t j = 0; j != 3; ++j) {
			res[i][j] = 0;
			for(int64_t k = 0; k != 3; ++k)
				res[i][j] += mat1[i][k] * mat2[k][j];
			res[i][j] %= MOD;
		}
	}
	for(int64_t i = 0; i != 3; ++i) {
		for(int64_t j = 0; j != 3; ++j)
			mat1[i][j] = res[i][j];
	}
}

int64_t Solve(int64_t x, int64_t y, int64_t z, int64_t a, int64_t b, int64_t c, int64_t n) {
	int64_t mat[3][3];
	mat[0][0] = 0; mat[0][1] = 0; mat[0][2] = c;
	mat[1][0] = 1; mat[1][1] = 0; mat[1][2] = b;
	mat[2][0] = 0; mat[2][1] = 1; mat[2][2] = a;

	int64_t res[3][3];
	res[0][0] = 1; res[0][1] = 0; res[0][2] = 0;
	res[1][0] = 0; res[1][1] = 1; res[1][2] = 0;
	res[2][0] = 0; res[2][1] = 0; res[2][2] = 1;

	for(; n; n >>= 1) {
		if(n & 1)
			MultMats(res, mat);
		MultMats(mat, mat);
	}

	return (x * res[0][0] + y * res[1][0] + z * res[2][0]) % MOD;
}

int main() {
	std::ifstream fin("iepuri.in");
	std::ofstream fout("iepuri.out");

	int64_t t;
	fin >> t;

	while(t--) {
		int64_t x, y, z, a, b, c, n;
		fin >> x >> y >> z >> a >> b >> c >> n;
		fout << Solve(x, y, z, a, b, c, n) << '\n';
	}

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

	return 0;
}