Cod sursa(job #34667)

Utilizator damaDamaschin Mihai dama Data 21 martie 2007 10:04:35
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.47 kb
#include <stdio.h>
#include <string.h>
#define modul 666013

void prod(long long a[3][3], long long b[3][3], long long c[3][3]);

int main()
{
	freopen("iepuri.in","r",stdin);
	freopen("iepuri.out","w",stdout);

	long long i, test, mat[32][3][3], a, b, c, x, y, z, n, sol[3][3], t, temp, cnt, j;

	scanf("%lld", &t);

	for(test = 1; test <= t; ++ test)
	{
		scanf("%lld%lld%lld%lld%lld%lld%lld", &x, &y, &z, &a, &b, &c, &n);
		if(n == 0)
		{
			printf("%lld\n", x);
			continue;
		}
		if(n == 1)
		{
			printf("%lld\n", y);
			continue;
		}
		if(n == 2)
		{
			printf("%lld\n", z);
			continue;
		}
		n -= 2;
		memset(mat, 0, sizeof(mat));
		memset(sol, 0, sizeof(sol));
		sol[0][0] = x;
		sol[1][0] = y;
		sol[2][0] = z;
		mat[0][0][1] = 1;
		mat[0][1][2] = 1;
		mat[0][2][0] = c;
		mat[0][2][1] = b;
		mat[0][2][2] = a;
		for(i = 1; i <= 30; ++i)
		{
			prod(mat[i], mat[i - 1], mat[i - 1]);
		}
		temp = 1 << 30;
		cnt = 30;
		while(n)
		{
			if(n & temp)
			{
				prod(sol, mat[cnt], sol); 
				n -= temp;
			}
			--cnt;
			temp >>= 1;
		}
		sol[2][0] %= modul;
		printf("%lld\n", sol[2][0]);
	}

	return 0;
}

void prod(long long a[3][3], long long b[3][3], long long c[3][3])
{
	long long i, j, k, temp[3][3];

	memset(temp, 0, sizeof(temp));

	for(i = 0; i < 3; ++i)
	{
		for(j = 0; j < 3; ++j)
		{
			for(k = 0; k < 3; ++k)
			{
				temp[i][j] += b[i][k] * c[k][j];
			}
			temp[i][j] %= modul;
		}
	}
	
	for(i = 0; i < 3; ++i)
	{
		for(j = 0; j < 3; ++j)
		{
			a[i][j] = temp[i][j];
		}
	}
}