Cod sursa(job #465)

Utilizator ProstuStefan-Alexandru Filip Prostu Data 11 decembrie 2006 12:54:39
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.69 kb
#include <cstdio>
#include <vector>
#include <cstdarg>

using namespace std;

const int MOD = 666013;

class matrice: protected vector <vector <int> > {
	private:
	int l, c;
	public:
	matrice();
	matrice(int l, int c, ...);
	matrice operator*(matrice);
	int getpos(int x, int y);
};

matrice::matrice() {
	l = c = 0;
}

matrice::matrice(int l, int c, ...) {
	va_list list;
	int i, j;

	this->l = l; this->c = c;

	this->resize(l);
	
	va_start(list, c);

	for (i = 0; i < l; ++i) {
		(*this)[i].resize(c);

		for (j = 0; j < c; ++j)
			(*this)[i][j] = va_arg(list, int);
	}

	va_end(list);
}

matrice matrice::operator*(matrice X) {
	matrice rez;
	int i, j, k;

	if (this->c != X.l) return rez;

	rez.l = this->l;
	rez.c = X.c;

	rez.resize(rez.l);
	for (i = 0; i < rez.l; ++i)
		rez[i].resize(rez.c);

	for (i = 0; i < rez.l; ++i)
		for (j = 0; j < X.c; ++j) {
			for (k = 0; k < X.l; ++k)
				rez[i][j] += ((long long) (*this)[i][k] * X[k][j]) % MOD;

			rez[i][j] %= MOD;
		}
	
	return rez;
}

int matrice::getpos(int x, int y) {
	if (x < 0 || x >= this->l) return -1;
	if (y < 0 || y >= this->c) return -1;

	return (*this)[x][y];
}

// END OF CLASS DESCRIPTION

int main() {
	FILE *fin = fopen("iepuri.in", "rt");
	FILE *fout = fopen("iepuri.out", "wt");
	int t, A, B, C, X, Y, Z, N;
	matrice M, Q;

	fscanf(fin, " %d", &t);
	while (t--) {
		fscanf(fin, " %d %d %d", &X, &Y, &Z);
		fscanf(fin, " %d %d %d", &A, &B, &C);
		fscanf(fin, " %d", &N);

		M = matrice(3, 3, 0, 1, 0, 0, 0, 1, C, B, A);
		Q = matrice(3, 3, 1, 0, 0, 0, 1, 0, 0, 0, 1);

		for (; N; N >>= 1, M = M * M)
			if (N & 1)
				Q = Q * M;

		Q = Q * matrice(3, 1, X, Y, Z);

		fprintf(fout, "%d\n", Q.getpos(0, 0));
	}

	fclose(fin);
	fclose(fout);
	return 0;
}