Cod sursa(job #1674983)

Utilizator andrei.mardaleAndrei Mardale andrei.mardale Data 4 aprilie 2016 23:40:32
Problema Iepuri Scor 0
Compilator java Status done
Runda Arhiva de probleme Marime 1.99 kb
//package iepuriDinamica;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

public class Main {
	
	public long[][] matrixMul (long[][] a, long[][] b){
		int i, j, k;
		long[][] r = new long[3][3];
		
		for (i = 0; i < 3; i++){
			for (j = 0; j < 3; j++){
				long s = 0;
				for (k = 0; k < 3; k++)
					s = s + a[i][k] * b[k][j];
				r[i][j] = s;
			}
		}
		return r;
	}

	public long[][] binaryMul (long[][] a, long n){
		if (n == 0){
			long[][] r = {{1,0,0}, {0,1,0}, {0,0,1}};
			return r;
		}
		if (n == 1)
			return a;
		if (n % 2 == 0){
			long [][] rez = binaryMul(a, n/2);
			return matrixMul(rez, rez);
		}
		if (n % 2 == 1){
			long[][] rez = binaryMul(a, n/2);
			return matrixMul(a, matrixMul(rez, rez));
		}
		return null;
	}
	
	public long solve (int x, int y, int z, int a, int b, int c, long n){
		long [][] A = { {0,1,0}, {0,0,1}, {c,b,a}};
		long [][] rez = binaryMul(A, n-2);
		long sum = rez[2][0] * x + rez[2][1] * y + rez[2][2] * z;
		
//		printM(rez);
		return sum%666013;
	}
	
	public void printM (int[][] m){
		for (int i = 0; i < 3; i++){
			for (int j = 0; j < 3; j++)
				System.out.print(m[i][j] + " ");
			System.out.println();
		}
	}
	
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new FileReader("iepuri.in"));
		PrintWriter pw = new PrintWriter("iepuri.out");
		String line = br.readLine();
		Main solver = new Main();
		int no_test = Integer.parseInt(line);
		for (int i = 0; i < no_test; i++){
			line = br.readLine();
			String[] splitted = line.split("\\s+");
			int x = Integer.parseInt(splitted[0]);
			int y = Integer.parseInt(splitted[1]);
			int z = Integer.parseInt(splitted[2]);
			int a = Integer.parseInt(splitted[3]);
			int b = Integer.parseInt(splitted[4]);
			int c = Integer.parseInt(splitted[5]);
			long n = Integer.parseInt(splitted[6]);
			
			pw.write(String.valueOf(solver.solve(x, y, z, a, b, c, n) + "\n"));
		}
		
		
		pw.close();
		br.close();
	}

}