Advertisement
bizzcuit

Bit Shift Matrix

Nov 26th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.math.BigInteger;
  4. import java.util.Arrays;
  5. import java.util.Scanner;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.         Scanner in = new Scanner(System.in);
  11.         int r = in.nextInt();
  12.         int c = in.nextInt();
  13.         int n = in.nextInt();
  14.         in.nextLine();
  15.  
  16.         String movements = in.nextLine();
  17.         String[] code = movements.split(" ");
  18.         double[] codes = Arrays.stream(code).mapToDouble(Double::parseDouble).toArray();
  19.  
  20.         int coeff = Math.max(r, c);
  21.  
  22.         BigInteger[][] matrix = new BigInteger[r][c];
  23.  
  24.         BigInteger counter = BigInteger.valueOf(1);
  25.         for (int row = r - 1; row >= 0; row--) {
  26.             for (int col = 0; col < c; col++) {
  27.                 matrix[row][col] = counter;
  28.                 if (col < c - 1) {
  29.                     counter = counter.multiply(BigInteger.valueOf(2));
  30.                 }
  31.             }
  32.             counter = matrix[row][1];
  33.         }
  34.  
  35. //        check matrix
  36. //        for (int row = 0; row < r; row++) {
  37. //            for (int col = 0; col < c; col++) {
  38. //                System.out.print(matrix[row][col] + " ");
  39. //            }
  40. //            System.out.println();
  41. //        }
  42.  
  43.         int currentRow = r - 1;
  44.         int currentCol = 0;
  45.         BigInteger sum = matrix[currentRow][currentCol];
  46.         matrix[currentRow][currentCol] = BigInteger.valueOf(0);
  47.  
  48.         for (int i = 0; i < codes.length; i++) {
  49.             int targetRow = (int)codes[i] / coeff;
  50.             int targetCol = (int)codes[i] % coeff;
  51.             while (targetCol != currentCol) {
  52.                 if (targetCol > currentCol) {
  53.                     currentCol++;
  54.                 } else {
  55.                     currentCol--;
  56.                 }
  57.                 sum = sum.add(matrix[currentRow][currentCol]);
  58.                 matrix[currentRow][currentCol] = BigInteger.valueOf(0);
  59.             }
  60.             while (targetRow != currentRow) {
  61.                 if (targetRow > currentRow) {
  62.                     currentRow++;
  63.                 } else {
  64.                     currentRow--;
  65.                 }
  66.                 sum = sum.add(matrix[currentRow][currentCol]);
  67.                 matrix[currentRow][currentCol] = BigInteger.valueOf(0);
  68.             }
  69.         }
  70.         System.out.println(sum);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement