Cod sursa(job #988812)

Utilizator helios11radu mihai helios11 Data 23 august 2013 21:41:20
Problema Iepuri Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.87 kb
#include <stdio.h>
#include <vector>
using namespace std;
#define mod 666013

int T, x0, x1, x2, A, B, C;
long long int N;
long long int** Mat;

long long int** identityMat(){
	long long int** Result = new long long int* [3];
	
	for(int i = 0; i < 3; ++i){
		Result[i] = new long long int [3];
		for(int j = 0; j < 3; ++j){
			if(i == j){
				Result[i][j] = 1;
			}
			else{
				Result[i][j] = 0;
			}
		}
	}
	
	return Result;
}

long long int** createMat(){
	long long int** Result = new long long int*[3];

	for(int i = 0; i < 3; ++i){
		Result[i] = new long long int[3];
	}

	//assign values
	Result[0][0] = A;
	Result[0][1] = B;
	Result[0][2] = C;
	Result[1][0] = 1;
	Result[1][1] = 0;
	Result[1][2] = 0;
	Result[2][0] = 0;
	Result[2][1] = 1;
	Result[2][2] = 0;

	return Result;
}

long long int** multMat(long long int** Mat1, int n, long long int** Mat2, int m){
	long long int** Result;
	if(n != m)
		return NULL;
	
	//allocate mem + assign init val
	Result = new long long int*[n];
	for(int i = 0; i < n; ++i){
		Result[i] = new long long int[n];
		for(int j = 0; j < n; ++j){
			Result[i][j] = 0;
		}
	}

	//calculate 
	for(int i = 0; i < n; ++i){
		for(int k = 0; k < m; ++k){
			for(int j = 0; j < n; ++j){
				Result[i][k] += (Mat1[i][j] * Mat2[j][k]) % mod;
			}
		}
	}
	
	return Result;
}

long long int** logPow(long long int** M, long long int P){
	long long int** Z = identityMat();

	while(P > 0){
		if(P % 2 == 1){
			Z = multMat(Z, 3, M, 3);
		}

		M = multMat(M, 3, M, 3);
		P = P / 2;
	}
	
	return Z;
}

void readSolve(){
	freopen("iepuri.in", "r", stdin);
	freopen("iepuri.out", "w", stdout);
	
	scanf("%d", &T);
	for(int i = 0; i < T; ++i){
		long long int** Result;
		long long int answer;

		scanf("%d %d %d %d %d %d %lld", &x0, &x1, &x2, &A, &B, &C, &N);
		Mat = createMat();
		Result = logPow(Mat, N-2);

		answer = ((Result[0][0] * x2) % mod + (Result[0][1] * x1) % mod + (Result[0][2] * x0) % mod) % mod; 

		printf("%lld\n", answer);
	}
}

int main(){
	readSolve();
	/*
	long long int** Mat1 = new long long int*[3];
	for(int i=0;i<3;++i){
		Mat1[i] = new long long int [3];
	}
	Mat1[0][0] = 2;
	Mat1[0][1] = 3;
	Mat1[0][2] = 1;
	Mat1[1][0] = 1;
	Mat1[1][1] = 4;
	Mat1[1][2] = 5;
	Mat1[2][0] = 2;
	Mat1[2][1] = 7;
	Mat1[2][2] = 9;

	long long int** Mat2 = new long long int*[3];
	for(int i=0;i<3;++i){
		Mat2[i] = new long long int [3];
	}
	Mat2[0][0] = 6;
	Mat2[0][1] = 1;
	Mat2[0][2] = 0;
	Mat2[1][0] = 8;
	Mat2[1][1] = 7;
	Mat2[1][2] = 7;
	Mat2[2][0] = 9;
	Mat2[2][1] = 6;
	Mat2[2][2] = 0;
	//long long int Mat2[3][3] = {{6, 1, 0}, {8,7,7}, {9,6,0}};
	
	Mat = multMat(Mat1,3, Mat2,3);
	freopen("iepuri.out", "w", stdout);
	
	//show mat
	for(int i=0;i<3; ++i){
		for(int j=0;j<3; ++j){
			printf("%lld ", Mat[i][j]);
		}
		printf("\n");
	}*/
	return 0;
}