The minimum cost of a path
Practice
2.5 (26 votes)
Algorithms
Dynamic programming
2d dynamic programming
Math
Prime factorization
Problem
93% Success 2187 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a 2D matrix containing \(N\) rows and \(M\) columns. You are currently standing at cell \((1,\ 1)\) and you want to reach cell \((n,\ m)\).

You can only move right or down from your current cell, that is, if you are currently in cell \((i,\ j)\), then you can either move to cell \((i+1,\ j)\) or cell \((i,\ j+1)\). You cannot move out of the matrix.

Each cell of the grid has some cost associated with it which you have to pay when you land at that cell.

Each cell has a number \(K\) associated with it. Now, the cost of that cell is defined as the maximum number of times you can divide \(K\) by a positive integer \(X\) that is greater than 1 until \(K\) becomes 1. Here, \(K\) must be divisible by \(X\).

The cost of a path is defined as the sum of the costs of all the cells in the path.

Note: You also have to pay the cost at cell \((1,\ 1)\) as you are standing on it.

You are required to determine the minimum cost to reach from cell \((1,\ 1)\) to cell \((n,\ m)\).

Input format

  • The first line of the input contains \(T\) denoting the number of test cases.
  • The first line of each test case contains two integers \(N\) and \(M\) denoting the number of rows and columns respectively.
  • The next \(N\) lines of each test case contain \(M\) integers representing the numbers on each cell of the grid.

Output format

For each test case, print a single line containing the minimum cost.

Constraints

\(1\le T\le 10000\\ 1\le N,\ M\le 1000\\ 1\le grid[i][j]\le 100000\\ Sum\ of\ (N*M)\ over\ all\ test\ cases\le 1000000\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
6 votes
Tags:
ImplementationAlgorithmsBasics of bit manipulationDynamic Programming2D dynamic programming
Points:30
12 votes
Tags:
CombinatoricsMathematicsDynamic Programming2D dynamic programmingAlgorithms
Points:30
22 votes
Tags:
AlgorithmsDynamic ProgrammingEasyString ManipulationTwo dimensional