Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int R = scanner.nextInt();
- int C = scanner.nextInt();
- int N = scanner.nextInt();
- int[] moves = new int[N];
- BigInteger[][] matrix = new BigInteger[R][C];
- BigInteger startValue = new BigInteger("1");
- for (int row = R -1; row >= 0; row--) {
- for (int col = 0; col < C; col++) {
- matrix[row][col] = startValue;
- if (col < C - 1) {
- startValue = startValue.multiply(BigInteger.valueOf(2));
- }
- }
- startValue = matrix[row][1];
- }
- // for (int i = R - 1; i >= 0 ; i--) {
- // for (int j = 0; j < C; j++) {
- // System.out.print( matrix[i][j] + " ");
- // }
- // System.out.println();
- // }
- int[] rowDirections = new int[moves.length];
- int[] colDirections = new int[moves.length];
- int COEF = Math.max(R,C);
- for (int i = 0; i < moves.length; i++) {
- moves[i] = scanner.nextInt();
- }
- int startRow = matrix.length - 1;
- int startCol = 0;
- BigInteger sum = matrix[startRow][startCol];
- matrix[startRow][startCol] = BigInteger.valueOf(0);
- for (int i = 0; i < moves.length; i++) {
- if (i == 0) {
- rowDirections[i] = (moves[i] / COEF) - startRow;
- colDirections[i] = moves[i] % COEF;
- } else {
- rowDirections[i] = moves[i] / COEF - moves[i - 1] / COEF;
- colDirections[i] = moves[i] % COEF - moves[i - 1] % COEF;
- }
- }
- for (int i = 0; i < N; i++) {
- if (colDirections[i] > 0) {
- for (int j = 0; j < colDirections[i]; j++) {
- startCol++;
- sum = sum.add(matrix[startRow][startCol]);
- matrix[startRow][startCol] = BigInteger.valueOf(0);
- }
- } else if (colDirections[i] < 0) {
- for (int j = 0; j > colDirections[i]; j--) {
- startCol--;
- sum = sum.add(matrix[startRow][startCol]);
- matrix[startRow][startCol] = BigInteger.valueOf(0);
- }
- }
- if (rowDirections[i] > 0) {
- for (int j = 0; j < rowDirections[i]; j++) {
- startRow++;
- sum = sum.add(matrix[startRow][startCol]);
- matrix[startRow][startCol] = BigInteger.valueOf(0);
- }
- } else if (rowDirections[i] < 0) {
- for (int j = 0; j > rowDirections[i]; j--) {
- startRow--;
- sum = sum.add(matrix[startRow][startCol]);
- matrix[startRow][startCol] = BigInteger.valueOf(0);
- }
- }
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement