Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int m = in.nextInt();
- long[][] matrix = new long[n][m];
- long startValue = 1;
- for (int row = 0; row < n; row++) {
- for (int col = 0; col < m; col++) {
- matrix[row][col] = startValue;
- if (col < m - 1) {
- startValue *= 2;
- }
- }
- startValue = matrix[row][1];
- }
- int dirRow = 1;
- int dirCol = 1;
- int currentRow = 0;
- int currentCol = 0;
- long sum = matrix[currentRow][currentCol];
- int nextRow = 0;
- int nextCol = 0;
- if (n == 1 || m == 1) {
- System.out.println(matrix[0][0]);
- } else {
- while (true) {
- nextRow = currentRow + dirRow;
- nextCol = currentCol + dirCol;
- if ((nextRow == 0 || nextRow == n - 1) && (nextCol == 0 || nextCol == m - 1)) {
- sum += matrix[nextRow][nextCol];
- System.out.println(sum);
- break;
- }
- if (nextRow == 0 || nextRow == n - 1) {
- dirRow *= -1;
- }
- if (nextCol == 0 || nextCol == m - 1) {
- dirCol *= -1;
- }
- currentRow = nextRow;
- currentCol = nextCol;
- sum += matrix[currentRow][currentCol];
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment