//Using matrix multiplication and divide and Conquer concepts
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < cases; i++) {
String[] str = br.readLine().split(" ");
int[][] seq = new int[2][1];
seq[1][0] = Integer.parseInt(str[0]);
seq[0][0] = Integer.parseInt(str[1]);
int[][] intial = {{1, 1}, {1, 0}};
int n = Integer.parseInt(str[2]);
int rem = (int) Math.pow(10, Integer.parseInt(str[3]));
int[][] powMat = matPow(intial, n, rem);
int[][] result = matrixMultiply(powMat, seq, rem);
sb.append(result[1][0]).append("\n");
}
System.out.print(sb);
}
public static int[][] matrixMultiply(int[][] x, int[][] y, int mod) {
int[][] temp = new int[x.length][y[0].length];
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[0].length; j++) {
for (int k = 0; k < x.length; temp[i][j] %= mod, k++) {
temp[i][j] += x[i][k] * y[k][j];
}
}
}
return temp;
}
public static int[][] matPow(int[][] matrix, int exponent, int mod) {
int[][] result = new int[matrix.length][matrix.length];
int[][] tempMatrix = new int[matrix.length][];
for (int i = 0; i < result.length; i++) {
result[i][i] = 1;
tempMatrix[i] = matrix[i].clone();
}
while (exponent > 0) {
if ((exponent & 1) == 1) {
result = matrixMultiply(result, tempMatrix, mod);
}
tempMatrix = matrixMultiply(tempMatrix, tempMatrix, mod);
exponent /= 2;
}
return result;
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cases = Integer.parseInt(br.readLine());
StringBuffer sb = new StringBuffer("");
for (int i = 0; i < cases; i++) {
String[] str = br.readLine().split(" ");
int[][] seq = new int[2][1];
seq[1][0] = Integer.parseInt(str[0]);
seq[0][0] = Integer.parseInt(str[1]);
int[][] intial = {{1, 1}, {1, 0}};
int n = Integer.parseInt(str[2]);
int rem = (int) Math.pow(10, Integer.parseInt(str[3]));
int[][] powMat = matPow(intial, n, rem);
int[][] result = matrixMultiply(powMat, seq, rem);
sb.append(result[1][0]).append("\n");
}
System.out.print(sb);
}
public static int[][] matrixMultiply(int[][] x, int[][] y, int mod) {
int[][] temp = new int[x.length][y[0].length];
for (int i = 0; i < temp.length; i++) {
for (int j = 0; j < temp[0].length; j++) {
for (int k = 0; k < x.length; temp[i][j] %= mod, k++) {
temp[i][j] += x[i][k] * y[k][j];
}
}
}
return temp;
}
public static int[][] matPow(int[][] matrix, int exponent, int mod) {
int[][] result = new int[matrix.length][matrix.length];
int[][] tempMatrix = new int[matrix.length][];
for (int i = 0; i < result.length; i++) {
result[i][i] = 1;
tempMatrix[i] = matrix[i].clone();
}
while (exponent > 0) {
if ((exponent & 1) == 1) {
result = matrixMultiply(result, tempMatrix, mod);
}
tempMatrix = matrixMultiply(tempMatrix, tempMatrix, mod);
exponent /= 2;
}
return result;
}
}
No comments:
Post a Comment