Cod sursa(job #583433)

Utilizator radubbRadu B radubb Data 20 aprilie 2011 10:07:36
Problema Iepuri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.22 kb
#include <cstdio>
using namespace std;

short x, y, z, a, b, c;
long n;
long m[4][4], M[4][4], MC[4][4];

void init(long x[][4])
{
	x[1][2] = x[2][3] = 1; 
	x[3][1] = c; x[3][2] = b; x[3][3] = a;

	for(short i=1; i<=3; i++)
		for(short j=1; j<=3; j++)
			MC[i][j] = 0;
}

void produs(long A[][4], long B[][4])
{
	for(short k=1; k<=3; k++)
		for(short i=1; i<=3; i++)
			for(short j=1; j<=3; j++)
				MC[k][i] += A[k][j] * B[j][i];
	for(short i=1; i<=3; i++)
		for(short j=1; j<=3; j++)
		{
			M[i][j] = MC[i][j];
			MC[i][j] = 0;
		}
	/*
	for(short i=1; i<=3; i++)
	{
		for(short j=1; j<=3; j++)
			printf("%ld ", M[i][j]);
		printf("\n");
	}
	printf("\n");
	*/
}

void m_la_n()
{
	while(n)
	{
		if(!n%2)
		{
			n /= 2;
			produs(M, M);
			
		}
		n--;
		produs(M, m);
	}
}

void afisare()
{	
	long sol = M[3][1]*x + M[3][2]*y + M[3][3]*z;
	printf("%ld\n", sol);
}

void citire()
{
	short T;
	freopen("iepuri.in","r",stdin); scanf("%hd", &T);
	for(; T; T--)
	{
		scanf("%hd %hd %hd %hd %hd %hd %ld", &x, &y, &z, &a, &b, &c, &n);
		
		n = n-3;
		init(m); init(M);
		m_la_n();
		afisare();
	}
}

int main()
{
	freopen("iepuri.out","w",stdout);
	citire(); 
	return 0;
}