Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Arrays;
- import java.util.Scanner;
- //Tazi zadacha me poburka
- public class Main {
- public static final Scanner sc = new Scanner(System.in);
- public static BigInteger[][] fulfillMatrix(BigInteger[][] matrix) {
- int row = matrix.length;
- int col = matrix[0].length;
- BigInteger num = BigInteger.valueOf(1);
- for (int i = row - 1; i >= 0; i--) {
- for (int j = 0; j < col; j++) {
- matrix[i][j] = num;
- num =num.multiply(BigInteger.valueOf(2));
- }
- num = matrix[i][1];
- }
- return matrix;
- }
- public static void main(String[] args) {
- int row = sc.nextInt();
- int col = sc.nextInt();
- long sum = 0;
- BigInteger sum1=BigInteger.valueOf(0);
- int max = Math.max(row, col);
- boolean[][] matrixCheck = new boolean[row][col];
- BigInteger[][] matrixNumbers = new BigInteger[row][col];
- fulfillMatrix(matrixNumbers);
- int number = sc.nextInt();
- int[] arr = new int[number];
- for (int i = 0; i < number; i++) {
- arr[i] = sc.nextInt();
- }
- int currentRow = row - 1;
- int currentCol = 0;
- for (int i = 0; i < number; i++) {
- row = arr[i] / max;
- col = arr[i] % max;
- if (col > currentCol && row == currentRow) {
- for (int j = currentCol; j <= col; j++) {
- if (!matrixCheck[currentRow][j]) {
- sum1=sum1.add(matrixNumbers[currentRow][j]);
- matrixCheck[currentRow][j] = true;
- }
- }
- currentRow = row;
- currentCol = col;
- continue;
- }
- if (col < currentCol && row == currentRow) {
- for (int j = currentCol; j >= col; j--) {
- if (!matrixCheck[currentRow][j]) {
- sum1=sum1.add(matrixNumbers[currentRow][j]);
- matrixCheck[currentRow][j] = true;
- }
- }
- currentRow = row;
- currentCol = col;
- continue;
- }
- if (col == currentCol && row > currentRow) {
- for (int j = currentRow; j <= row; j++) {
- if (!matrixCheck[j][currentCol]) {
- sum1=sum1.add(matrixNumbers[j][currentCol]);
- matrixCheck[j][currentCol] = true;
- }
- }
- currentRow = row;
- currentCol = col;
- continue;
- }
- if (col == currentCol && row < currentRow) {
- for (int j = currentRow; j >= row; j--) {
- if (!matrixCheck[j][currentCol]) {
- sum1=sum1.add(matrixNumbers[j][currentCol]);
- matrixCheck[j][currentCol] = true;
- }
- }
- currentRow = row;
- currentCol = col;
- continue;
- }
- //row++
- for (int j = currentCol; j <= col; j++) {
- if (!matrixCheck[currentRow][j]) {
- sum1=sum1.add(matrixNumbers[currentRow][j]);
- matrixCheck[currentRow][j] = true;
- }
- }
- //row--
- for (int j = currentCol; j >= col; j--) {
- if (!matrixCheck[currentRow][j]) {
- sum1=sum1.add(matrixNumbers[currentRow][j]);
- matrixCheck[currentRow][j] = true;
- }
- }
- currentCol = col;
- //col-
- for (int j = currentRow; j >= row; j--) {
- if (!matrixCheck[j][currentCol]) {
- sum1=sum1.add(matrixNumbers[j][currentCol]);
- matrixCheck[j][currentCol] = true;
- }
- }
- //col+
- for (int j = currentRow; j <= row; j++) {
- if (!matrixCheck[j][currentCol]) {
- sum1=sum1.add(matrixNumbers[j][currentCol]);
- matrixCheck[j][currentCol] = true;
- }
- }
- currentRow = row;
- }
- System.out.println(sum1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement