Advertisement
valitomi

Untitled

May 1st, 2021
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.59 KB | None | 0 0
  1. import java.math.BigInteger;
  2. import java.util.Arrays;
  3. import java.util.Scanner;
  4.  
  5.  
  6. //Tazi zadacha me poburka
  7.  
  8.  
  9. public class Main {
  10. public static final Scanner sc = new Scanner(System.in);
  11.  
  12. public static BigInteger[][] fulfillMatrix(BigInteger[][] matrix) {
  13.  
  14.  
  15. int row = matrix.length;
  16. int col = matrix[0].length;
  17. BigInteger num = BigInteger.valueOf(1);
  18. for (int i = row - 1; i >= 0; i--) {
  19. for (int j = 0; j < col; j++) {
  20. matrix[i][j] = num;
  21. num =num.multiply(BigInteger.valueOf(2));
  22. }
  23. num = matrix[i][1];
  24. }
  25. return matrix;
  26.  
  27. }
  28.  
  29.  
  30. public static void main(String[] args) {
  31. int row = sc.nextInt();
  32. int col = sc.nextInt();
  33. long sum = 0;
  34. BigInteger sum1=BigInteger.valueOf(0);
  35.  
  36. int max = Math.max(row, col);
  37.  
  38. boolean[][] matrixCheck = new boolean[row][col];
  39. BigInteger[][] matrixNumbers = new BigInteger[row][col];
  40.  
  41. fulfillMatrix(matrixNumbers);
  42.  
  43.  
  44.  
  45.  
  46. int number = sc.nextInt();
  47. int[] arr = new int[number];
  48. for (int i = 0; i < number; i++) {
  49. arr[i] = sc.nextInt();
  50. }
  51.  
  52.  
  53. int currentRow = row - 1;
  54. int currentCol = 0;
  55.  
  56. for (int i = 0; i < number; i++) {
  57. row = arr[i] / max;
  58. col = arr[i] % max;
  59.  
  60.  
  61. if (col > currentCol && row == currentRow) {
  62. for (int j = currentCol; j <= col; j++) {
  63. if (!matrixCheck[currentRow][j]) {
  64.  
  65. sum1=sum1.add(matrixNumbers[currentRow][j]);
  66. matrixCheck[currentRow][j] = true;
  67. }
  68. }
  69. currentRow = row;
  70. currentCol = col;
  71. continue;
  72.  
  73. }
  74.  
  75. if (col < currentCol && row == currentRow) {
  76. for (int j = currentCol; j >= col; j--) {
  77. if (!matrixCheck[currentRow][j]) {
  78.  
  79. sum1=sum1.add(matrixNumbers[currentRow][j]);
  80. matrixCheck[currentRow][j] = true;
  81. }
  82. }
  83. currentRow = row;
  84. currentCol = col;
  85. continue;
  86. }
  87.  
  88.  
  89. if (col == currentCol && row > currentRow) {
  90. for (int j = currentRow; j <= row; j++) {
  91. if (!matrixCheck[j][currentCol]) {
  92.  
  93. sum1=sum1.add(matrixNumbers[j][currentCol]);
  94. matrixCheck[j][currentCol] = true;
  95. }
  96. }
  97. currentRow = row;
  98. currentCol = col;
  99. continue;
  100. }
  101.  
  102.  
  103.  
  104. if (col == currentCol && row < currentRow) {
  105. for (int j = currentRow; j >= row; j--) {
  106. if (!matrixCheck[j][currentCol]) {
  107.  
  108. sum1=sum1.add(matrixNumbers[j][currentCol]);
  109. matrixCheck[j][currentCol] = true;
  110. }
  111. }
  112. currentRow = row;
  113. currentCol = col;
  114. continue;
  115. }
  116.  
  117. //row++
  118. for (int j = currentCol; j <= col; j++) {
  119. if (!matrixCheck[currentRow][j]) {
  120.  
  121. sum1=sum1.add(matrixNumbers[currentRow][j]);
  122. matrixCheck[currentRow][j] = true;
  123. }
  124. }
  125. //row--
  126. for (int j = currentCol; j >= col; j--) {
  127. if (!matrixCheck[currentRow][j]) {
  128.  
  129. sum1=sum1.add(matrixNumbers[currentRow][j]);
  130. matrixCheck[currentRow][j] = true;
  131. }
  132. }
  133. currentCol = col;
  134. //col-
  135. for (int j = currentRow; j >= row; j--) {
  136. if (!matrixCheck[j][currentCol]) {
  137.  
  138. sum1=sum1.add(matrixNumbers[j][currentCol]);
  139. matrixCheck[j][currentCol] = true;
  140. }
  141. }
  142. //col+
  143. for (int j = currentRow; j <= row; j++) {
  144. if (!matrixCheck[j][currentCol]) {
  145.  
  146. sum1=sum1.add(matrixNumbers[j][currentCol]);
  147. matrixCheck[j][currentCol] = true;
  148. }
  149. }
  150.  
  151.  
  152.  
  153.  
  154. currentRow = row;
  155.  
  156. }
  157.  
  158. System.out.println(sum1);
  159.  
  160.  
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement