#C8884. Unique Paths in a Grid

    ID: 52915 Type: Default 1000ms 256MiB

Unique Paths in a Grid

Unique Paths in a Grid

You are given a grid with m rows and n columns. Your task is to calculate the number of unique paths from the top-left corner to the bottom-right corner of the grid. At each step, you may only move either right or down.

The number of unique paths can be computed using dynamic programming with the recurrence relation:

$$dp[i][j] = dp[i-1][j] + dp[i][j-1]$$

If either m or n is 1, there is exactly 1 unique path.

For example:

  • For a grid of size 3 x 7, the answer is 28.
  • For a grid of size 2 x 2, the answer is 2.

Read the input from stdin and output the result to stdout.

inputFormat

The input consists of a single line containing two space-separated integers m and n, representing the number of rows and columns of the grid respectively.

outputFormat

Output a single integer which is the number of unique paths from the top-left corner to the bottom-right corner of the grid.## sample

3 7
28