Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int rows = Integer.parseInt(scanner.nextLine());
- int columns = Integer.parseInt(scanner.nextLine());
- int moves = Integer.parseInt(scanner.nextLine());
- BigInteger[][] matrix = new BigInteger[rows][columns];
- for (int row = rows - 1; row >= 0; row--) {
- for (int col = 0; col < columns; col++) {
- int current = (int) Math.pow(2, col + (rows - 1 - row));
- matrix[row][col] = BigInteger.valueOf(current);
- }
- }
- BigInteger sum = BigInteger.ONE;
- sum.add(matrix[rows - 1][0]);
- int targerRow = 0;
- int targetCol = 0;
- int currentRow = rows - 1;
- int currentCol = 0;
- int coef = Math.max(rows, columns);
- String[] line = scanner.nextLine().split(" ");
- int[] directions = new int[moves];
- for (int i = 0; i < directions.length; i++) {
- directions[i] = Integer.parseInt(line[i]);
- targerRow = directions[i] / coef;
- targetCol = directions[i] % coef;
- if (currentCol < targetCol) {
- for (int c = currentCol + 1; c <= targetCol; c++) {
- sum = sum.add(matrix[currentRow][c]);
- matrix[currentRow][c] = BigInteger.valueOf(0);
- }
- } else {
- for (int c = currentCol - 1; c >= targetCol; c--) {
- sum = sum.add(matrix[currentRow][c]);
- matrix[currentRow][c] = BigInteger.valueOf(0);
- }
- }
- currentCol = targetCol;
- if (currentRow < targerRow) {
- for (int r = currentRow + 1; r <= targerRow; r++) {
- sum = sum.add(matrix[r][currentCol]);
- matrix[r][currentCol] = BigInteger.valueOf(0);
- }
- } else {
- for (int r = currentRow - 1; r >= targerRow; r--) {
- sum = sum.add(matrix[r][currentCol]);
- matrix[r][currentCol] = BigInteger.valueOf(0);
- }
- }
- currentRow = targerRow;
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement