Cod sursa(job #630048)

Utilizator d.andreiDiaconeasa Andrei d.andrei Data 4 noiembrie 2011 16:26:06
Problema Iepuri Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.01 kb
#include <cstdio>
#include <cstring>

#define file_in "iepuri.in"
#define file_out "iepuri.out"

#define mod 666013
#define ll long long 

int T;
ll X,Y,Z,A,B,C,N;
ll sol[3][3];
ll m[3][3];

void mul(ll a[3][3],ll b[3][3]){
	int i,j,k;
	ll c[3][3];
	memset(c,0,sizeof(c));
	
	for (i=0;i<3;++i)
		 for (j=0;j<3;++j)
			  for (k=0;k<3;++k)
				   c[i][j]=(1LL*c[i][j]+1LL*a[i][k]*b[k][j])%mod;
	memcpy(a,c,sizeof(c));
}	

int main(){
	
	freopen(file_in,"r",stdin);
	freopen(file_out,"w",stdout);
	
	scanf("%d", &T);
	
	while(T--){
		
		scanf("%lld %lld %lld %lld %lld %lld %lld", &X, &Y, &Z, &A, &B, &C, &N);
		
		sol[0][0]=1;
		sol[1][1]=1;
		sol[2][2]=1;
		m[0][0]=0;
		m[0][1]=0;
		m[0][2]=C;
		m[1][0]=1;
		m[1][1]=0;
		m[1][2]=B;
		m[2][0]=0;
		m[2][1]=1;
		m[2][2]=A;
		
		N-=2;
		
		while(N){
			if (N%2)
				mul(sol,m);
			mul(m,m);
			N/=2;
		}
		
		printf("%lld\n", ((1LL*Z*sol[2][2])%mod+(1LL*Y*sol[1][2])%mod+(1LL*sol[0][2]*X)%mod)%mod);
		
	}
	
	return 0;
	
}