Cod sursa(job #1539608)

Utilizator ionutmodoModoranu Ionut-Vlad ionutmodo Data 1 decembrie 2015 02:04:02
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.27 kb
/*
	http://www.infoarena.ro/problema/iepuri
*/

#include <fstream>
#include <iostream>
#include <cstring>
#define MOD 666013
using namespace std;

void multiply(int A[][3], int B[][3], int C[][3])
{
	memset(C, 0, sizeof(C));
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 3; j++)
			for (int k = 0; k < 3; ++k)
				C[i][j] = (C[i][j] * 1LL + A[i][k] * B[k][j] * 1LL) % MOD;
}

int power(int X, int Y, int Z, int A, int B, int C, int N)
{
	int Mk[3][3], solution[3][3], result[3][3];
	Mk[0][0] = A; Mk[0][1] = B; Mk[0][2] = C;
	Mk[1][0] = 1; Mk[1][1] = 0; Mk[1][2] = 0;
	Mk[2][0] = 0; Mk[2][1] = 1; Mk[2][2] = 0;
	for (int i = 0; i < 3; ++i)
		for (int j = 0; j < 3; ++j)
			solution[i][j] = (i == j);
	for (N = N - 2; N; N >>= 1)
	{
		if (N & 1)
		{
			multiply(solution, Mk, result);
			memcpy(solution, result, sizeof(result));
		}
		multiply(Mk, Mk, result);
		memcpy(Mk, result, sizeof(result));
	}
	return (Z * solution[0][0] + Y * solution[0][1] + X * solution[0][2]);
}

int main()
{
	int T, X, Y, Z, A, B, C, N;
	ofstream fout("iepuri.out");
	ifstream fin("iepuri.in");
	fin >> T;
	while (T--)
	{
		fin >> X >> Y >> Z >> A >> B >> C >> N;
		fout << power(X, Y, Z, A, B, C, N) << "\n";
	}
	fin.close();
	fout.close();
	return 0;
}