Advertisement
Mishakis

BitShiftMatrix

Nov 25th, 2018
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. import java.math.BigInteger;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int R = scanner.nextInt();
  10.         int C = scanner.nextInt();
  11.         int N = scanner.nextInt();
  12.         int[] moves = new int[N];
  13.  
  14.  
  15.         BigInteger[][] matrix = new BigInteger[R][C];
  16.  
  17.  
  18.         BigInteger startValue = new BigInteger("1");
  19.         for (int row = R -1; row >= 0; row--) {
  20.             for (int col = 0; col < C; col++) {
  21.                 matrix[row][col] = startValue;
  22.                 if (col < C - 1) {
  23.                     startValue = startValue.multiply(BigInteger.valueOf(2));
  24.                 }
  25.             }
  26.             startValue = matrix[row][1];
  27.         }
  28.  
  29.  
  30. //        for (int i = R - 1; i >= 0 ; i--) {
  31. //            for (int j = 0; j < C; j++) {
  32. //                System.out.print( matrix[i][j] + " ");
  33. //            }
  34. //            System.out.println();
  35. //        }
  36.  
  37.  
  38.         int[] rowDirections = new int[moves.length];
  39.         int[] colDirections = new int[moves.length];
  40.         int COEF = Math.max(R,C);
  41.  
  42.  
  43.         for (int i = 0; i < moves.length; i++) {
  44.             moves[i] = scanner.nextInt();
  45.         }
  46.         int startRow = matrix.length - 1;
  47.         int startCol = 0;
  48.         BigInteger sum = matrix[startRow][startCol];
  49.         matrix[startRow][startCol] = BigInteger.valueOf(0);
  50.  
  51.  
  52.  
  53.  
  54.         for (int i = 0; i < moves.length; i++) {
  55.             if (i == 0) {
  56.                 rowDirections[i] = (moves[i] / COEF) - startRow;
  57.                 colDirections[i] = moves[i] % COEF;
  58.             } else {
  59.                 rowDirections[i] = moves[i] / COEF - moves[i - 1] / COEF;
  60.                 colDirections[i] = moves[i] % COEF - moves[i - 1] % COEF;
  61.             }
  62.         }
  63.  
  64.             for (int i = 0; i < N; i++) {
  65.                 if (colDirections[i] > 0) {
  66.                     for (int j = 0; j < colDirections[i]; j++) {
  67.                         startCol++;
  68.                         sum = sum.add(matrix[startRow][startCol]);
  69.                         matrix[startRow][startCol] = BigInteger.valueOf(0);
  70.                     }
  71.                 } else if (colDirections[i] < 0) {
  72.                     for (int j = 0; j > colDirections[i]; j--) {
  73.                         startCol--;
  74.                         sum = sum.add(matrix[startRow][startCol]);
  75.                         matrix[startRow][startCol] = BigInteger.valueOf(0);
  76.                     }
  77.                 }
  78.                 if (rowDirections[i] > 0) {
  79.                     for (int j = 0; j < rowDirections[i]; j++) {
  80.                         startRow++;
  81.                         sum = sum.add(matrix[startRow][startCol]);
  82.                         matrix[startRow][startCol] = BigInteger.valueOf(0);
  83.                     }
  84.                 } else if (rowDirections[i] < 0) {
  85.                     for (int j = 0; j > rowDirections[i]; j--) {
  86.                         startRow--;
  87.                         sum = sum.add(matrix[startRow][startCol]);
  88.                         matrix[startRow][startCol] = BigInteger.valueOf(0);
  89.                     }
  90.                 }
  91.  
  92.  
  93.  
  94.  
  95.         }
  96.         System.out.println(sum);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement