Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.math.BigInteger;
- public class Task {
- public static void main(String[] args) throws IOException {
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int rows = Integer.parseInt(br.readLine()); // between 1 and 100
- int cols = Integer.parseInt(br.readLine()); // between 1 and 75
- int movesNum = Integer.parseInt(br.readLine()); // between 1 and 1000
- String[] codeNumbers = br.readLine().split(" ");
- int coefficient = Math.max(rows, cols);
- int posX = 0;
- int posY = 0;
- int currentPosX = 4;
- int currentPosY = 0;
- int m = 0;
- int n = 0;
- int power = 0;
- int counter = 1;
- BigInteger sum = new BigInteger("1");
- // boolean matrix - false
- boolean[][] bool = new boolean[rows][cols];
- // matrix - power of 2
- BigInteger[][] matrix = new BigInteger[rows][cols];
- for (int i = rows - 1; i >= 0; i--) {
- for (int j = 0; j < cols; j++) {
- matrix[i][j] = BigInteger.valueOf((long)Math.pow(2, power));
- power++;
- }
- power = counter;
- counter++;
- }
- // print matrix - power of 2
- for (int i = 0; i < rows; i++) {
- for (int j = 0; j < cols; j++) {
- // System.out.print(matrix[i][j] + " ");
- }
- // System.out.println();
- }
- // moves array
- double[] decArray = new double[movesNum];
- for (int i = 0; i < movesNum; i++) {
- decArray[i] = Double.parseDouble(codeNumbers[i]);
- posX = (int)(decArray[i] / coefficient);
- posY = (int)(decArray[i] % coefficient);
- while (currentPosY != posY) {
- if (currentPosY < posY) {
- m = currentPosX;
- n = currentPosY + 1;
- if (bool[m][n] == false) {
- sum = sum.add(matrix[m][n]);
- bool[m][n] = true;
- }
- currentPosY = n;
- } else {
- m = currentPosX;
- n = currentPosY - 1;
- if (bool[m][n] == false) {
- sum = sum.add(matrix[m][n]);
- bool[m][n] = true;
- }
- currentPosY = n;
- }
- }
- while (currentPosX != posX) {
- if (currentPosX > posX) {
- m = currentPosX - 1;
- if (bool[m][n] == false) {
- sum = sum.add(matrix[m][n]);
- bool[m][n] = true;
- }
- currentPosX = m;
- }
- if (currentPosX < posX) {
- m = currentPosX + 1;
- if (bool[m][n] == false) {
- sum = sum.add(matrix[m][n]);
- bool[m][n] = true;
- }
- currentPosX = m;
- }
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement