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 sc = new Scanner(System.in);
- int a = sc.nextInt();
- int b = sc.nextInt();
- if(a==1||b==1){
- System.out.println(1);
- } else {
- long[][] matrix = new long[a][b];
- long num = 1;
- long sum = 1;
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[matrix.length - 1].length; j++) {
- matrix[i][j] = num;
- num *= 2;
- }
- num = matrix[i][1];
- }
- int i = 0;
- int j = 0;
- int c = 1;
- int d = 1;
- while (true) {
- if (i == a - 1) {
- c *= -1;
- }
- if (i == 0) {
- c = 1;
- }
- i = i + c;
- if (j == b - 1) {
- d *= -1;
- }
- if (j == 0) {
- d = 1;
- }
- j = j + d;
- sum += matrix[i][j];
- if ((i == 0 && j == 0) || (i == a - 1 && j == b - 1) || (i == a - 1 && j == 0) || (j == b - 1 && i == 0)) {
- System.out.println(sum);
- break;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment