Cod sursa(job #683136)

Utilizator mika17Mihai Alex Ionescu mika17 Data 20 februarie 2012 03:39:35
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.54 kb
#include <fstream>
#include <cstring>
#include <vector>
using std::vector;

class matrix
{
private:

	vector< vector<long long> > a;
	unsigned rows,cols;

public:
	template<unsigned R,unsigned C>
	matrix& assign(long long (*b)[C])
	{
		rows = R;
		cols = C;
		a.resize(R);
		for(unsigned i = 0; i < R; ++i)
			a[i].assign(b[i],b[i] + C);
		return *this;
	}
	const vector< long long > & operator[](unsigned i) const //posibile probleme cu eficienta aici
	{
		return a[i];
	}
	matrix& operator*=(const matrix &m) // *= deci nu poate fi const
	{
		vector< vector<long long> > c(rows,vector<long long>(m.cols,0));

		for(unsigned i = 0; i < rows; ++i)
			for(unsigned j = 0; j < m.cols; ++j)
				for(unsigned k = 0; k < cols; ++k)
					(c[i][j] += a[i][k] * m[k][j]) %= 666013;

		a = c;

		return *this;
	}
	matrix& pow(unsigned p) // ^= deci nu poate fi const
	{
		long long b[3][3] = {{1,0,0},{0,1,0},{0,0,1}};
		matrix c(*this); //std::vector foloseste deep-copy
		this->assign<3,3>(b);

		for(unsigned i = 0; i < 32; ++i)
		{
			if( (p & 1 << i) != 0 )
				*this *= c;
			c *= c;
		}
		return *this;
	}
};

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

	unsigned T;
	in >> T;
	while(T--)
	{
		long long X,Y,Z,A,B,C;
		unsigned N;
		in >> X >> Y >> Z >> A >> B >> C >> N;
		long long a[3][3] = {{A,B,C},{1,0,0},{0,1,0}};
		long long b[3][1] = {{Z},{Y},{X}};
		out << ( matrix().assign<3,3>(a) . pow( N - 2 ) *= matrix().assign<3,1>(b) )[0][0] << '\n';
	}
	return 0;
}