Strange Grid

#codeifyoucansolve
A strange grid has been recovered from an old book. It has 5 columns and infinite number of rows. The bottom row is considered as the first row. First few rows of the grid are like this:

…………..

…………..

20 22 24 26 28

11 13 15 17 19

10 12 14 16 18

1 3 5 7 9

0 2 4 6 8

The grid grows upwards forever!

Your task is to find the integer in cth column in rth row of the grid.

Input Format

There will be two integers r and c separated by a single space.

Constraints

1≤r≤2 * 109

1≤c≤5

Rows are indexed from bottom to top and columns are indexed from left to right.

Output Format

Output the answer in a single line.

Sample Input

6 3

Sample Output

25

Explanation

The number in the 6th row and 3rd column is 25.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger stval;
        BigInteger mod = new BigInteger("2");
        BigInteger ten = new BigInteger("10");
        BigInteger one = new BigInteger("1");

        BigInteger bg=new BigInteger(sc.next());
        int c;
        c=sc.nextInt();
        if(bg.remainder(mod).intValue() == 0)
            {
            bg=bg.subtract(one);
            bg=bg.divide(mod);
            bg=bg.multiply(ten);
            bg=bg.add(one);
            stval = new BigInteger(bg.toString());
        }
        else
            {
            bg=bg.divide(mod);
            bg=bg.multiply(ten);
            stval = new BigInteger(bg.toString());
        }
        c = (c-1)*2;
        String str = String.valueOf(c);
        System.out.println(stval.add(new BigInteger(str)));
    }
}

Follow on Twitter : https://twitter.com/codeifucansolve