Cod sursa(job #233335)

Utilizator peanutzAndrei Homorodean peanutz Data 17 decembrie 2008 16:12:17
Problema Iepuri Scor 90
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.24 kb
#include <stdio.h>
#include <string.h>

#define MOD 666013

int t, x, y, z, a, b, c, n;
long long m[4][4], crt[4][4];
long long aux[4][4];

void mul(long long a[4][4], long long b[4][4])
{
	int i, j, k;
	memset(aux, 0, sizeof(aux));
	for(i = 1; i <= 3; ++i)
		for(j = 1; j <= 3; ++j)
			for(k = 1; k <= 3; ++k)
				aux[i][j] = (aux[i][j] + (a[i][k]*b[k][j]) % MOD) % MOD;
			
	memcpy(a, aux, sizeof(aux));
}

int main()
{
	freopen("iepuri.in", "r", stdin);
	freopen("iepuri.out", "w", stdout);
	
	scanf("%d", &t);
	while(t--)
	{
		scanf("%d %d %d %d %d %d %d", &x, &y, &z, &a, &b, &c, &n);
		if(n == 0)
		{
			printf("%d\n", x);
			continue;
		}
		if(n == 1) 
		{
			printf("%d\n", y);
			continue;
		}
		if(n == 2)
		{
			printf("%d\n", z);
			continue;
		}
		
		crt[1][1] = 0; crt[1][2] = 0; crt[1][3] = c;
		crt[2][1] = 1; crt[2][2] = 0; crt[2][3] = b;
		crt[3][1] = 0; crt[3][2] = 1; crt[3][3] = a;
		
		int ok = 0;
		int val = 1;
		for(; val <= n; val <<= 1)
		{
			if(n & val)
			{
				if(!ok)
					memcpy(m, crt, sizeof(crt)), ok = 1;
				else
					mul(m, crt);
			}
			mul(crt, crt);
		}
		printf("%lld\n", ((x*m[1][1])%MOD + (y*m[2][1])%MOD + (z*m[3][1])%MOD) % MOD);
	} 
	return 0;
}