Advertisement
LiliyaBurlakova

Last

Dec 8th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package com.company;
  2.  
  3.         import java.math.BigInteger;
  4.         import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         int rows = Integer.parseInt(scanner.nextLine());
  11.         int columns = Integer.parseInt(scanner.nextLine());
  12.         int moves = Integer.parseInt(scanner.nextLine());
  13.  
  14.         BigInteger[][] matrix = new BigInteger[rows][columns];
  15.         for (int row = rows - 1; row >= 0; row--) {
  16.             for (int col = 0; col < columns; col++) {
  17.                 int current = (int) Math.pow(2, col + (rows - 1 - row));
  18.                 matrix[row][col] = BigInteger.valueOf(current);
  19.             }
  20.         }
  21.  
  22.         BigInteger sum = BigInteger.ONE;
  23.         sum.add(matrix[rows - 1][0]);
  24.         int targerRow = 0;
  25.         int targetCol = 0;
  26.         int currentRow = rows - 1;
  27.         int currentCol = 0;
  28.         int coef = Math.max(rows, columns);
  29.  
  30.         String[] line = scanner.nextLine().split(" ");
  31.         int[] directions = new int[moves];
  32.         for (int i = 0; i < directions.length; i++) {
  33.             directions[i] = Integer.parseInt(line[i]);
  34.             targerRow = directions[i] / coef;
  35.             targetCol = directions[i] % coef;
  36.             if (currentCol < targetCol) {
  37.                 for (int c = currentCol + 1; c <= targetCol; c++) {
  38.                     sum = sum.add(matrix[currentRow][c]);
  39.                     matrix[currentRow][c] = BigInteger.valueOf(0);
  40.                 }
  41.             } else {
  42.                 for (int c = currentCol - 1; c >= targetCol; c--) {
  43.                     sum = sum.add(matrix[currentRow][c]);
  44.                     matrix[currentRow][c] = BigInteger.valueOf(0);
  45.                 }
  46.             }
  47.             currentCol = targetCol;
  48.             if (currentRow < targerRow) {
  49.                 for (int r = currentRow + 1; r <= targerRow; r++) {
  50.                     sum = sum.add(matrix[r][currentCol]);
  51.                     matrix[r][currentCol] = BigInteger.valueOf(0);
  52.                 }
  53.             } else {
  54.                 for (int r = currentRow - 1; r >= targerRow; r--) {
  55.                     sum = sum.add(matrix[r][currentCol]);
  56.                     matrix[r][currentCol] = BigInteger.valueOf(0);
  57.                 }
  58.             }
  59.             currentRow = targerRow;
  60.         }
  61.         System.out.println(sum);
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement