Product Path You are given two integers N N and ...
Créé le : 26 mars 2025
Créé le : 26 mars 2025
Product Path
You are given two integers
N
N and
M
M.
Consider two infinite arrays
A
A and
B
B, where
[
6
,
3
,
3
,
0
,
0
,
6
,
0
,
0
,
0
,
…
]
A=[6,3,3,0,0,6,0,0,0,…]
You are standing on the 2D plane, at point
(
1
,
1
)
(1,1).
In one move, you can move in any of the four directions, as long as both coordinates remain positive. That is, from point
(
x
,
y
)
(x,y), you can move to
(
x
−
1
,
y
)
,
(
x
+
1
,
y
)
,
(
x
,
y
−
1
)
,
(x−1,y),(x+1,y),(x,y−1), or
(
x
,
y
+
1
)
(x,y+1), as long as both coordinates of the destination cell are positive.
The cost of point
(
x
,
y
)
(x,y) is defined to be
A
x
×
B
y
A
x
×B
y
.
Find the minimum total cost of moving from
(
1
,
1
)
(1,1) to
(
P
,
Q
)
(P,Q), where the total cost of a path is the sum of costs of all points visited along the way (including the initial and final points).
Input Format
The first line of input will contain a single integer
T
T, denoting the number of test cases.
Each test case consists of a single line of input, containing four space-separated integers
N
,
M
,
P
,
N,M,P, and
Q
Q as described in the statement.
Output Format
For each test case, output on a new line the answer: the minimum cost to reach
(
P
,
Q
)
(P,Q).
Constraints
1
≤
T
≤
1
0
3
1≤T≤10
3
1
≤
N
,
M
,
P
,
Q
≤
1
0
8
1≤N,M,P,Q≤10
8
2
A
2
=max(2,
2
2
)=2 while all following elements are
0
0. The same applies to array
B
B.
12
2×2+2×2+2×2=12.
Test case
2
2: Every path from
(
1
,
1
)
(1,1) to
(
3
,
10
)
(3,10) has a cost of
4
4.
accepted
Accepted
2
total-Submissions
Submissions
5
accuracy
Accuracy
40
Did you like the problem statement?
More Info
Time limit2 secs
Memory limit1.5 GB
Source Limit50000 Bytes
import java.util.;
import java.lang.;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
}
}
write a java solution
To solve this problem, we need to focus on the two key elements:
A[x] * B[y]
.(1,1)
to (P,Q)
.A[i]
is defined as max(i, N / i)
if i
divides N
; otherwise A[i] = 0
.B[i]
is defined as max(i, M / i)
if i
divides M
; otherwise B[i] = 0
.Given this, the cost to move from (x, y)
is A[x] * B[y]
.
Calculate the values for A and B:
A[x]
, calculate the maximum of x
and N / x
if x
divides N
. Otherwise, set A[x] = 0
.B[y]
, calculate B[y]
based on M
.Dynamic Programming Approach: Since the movement is allowed in four directions (up, down, left, right), a simple dynamic programming solution should work where we calculate the minimum cost to reach each point on the grid.
(1,1)
and initialize the cost to get there as A[1] * B[1]
.(x, y)
using a BFS-like strategy (or dynamic programming) to explore the points.
idea is to update the minimum cost by considering the neighbors and the cost to move to them.Here's how you can write a Java solution for the problem:
javaimport java.util.*; class Codechef { static int[] A, B; // Function to compute A[i] based on N static void computeA(int N) { A = new int[N + 1]; for (int i = 1; i <= N; i++) { if (N % i == 0) { A[i] = Math.max(i, N / i); } else { A[i] = 0; } } } // Function to compute B[i] based on M static void computeB(int M) { B = new int[M + 1]; for (int i = 1; i <= M; i++) { if (M % i == 0) { B[i] = Math.max(i, M / i); } else { B[i] = 0; } } } public static void main (String[] args) throws java.lang.Exception { Scanner sc = new Scanner(System.in); // Number of test cases int T = sc.nextInt(); while (T-- > 0) { // Reading N, M, P, Q for each test case int N = sc.nextInt(); int M = sc.nextInt(); int P = sc.nextInt(); int Q = sc.nextInt(); // Compute A[] and B[] computeA(N); computeB(M); // We use a BFS to find the minimum cost path Queue<int[]> queue = new LinkedList<>(); boolean[][] visited = new boolean[P + 1][Q + 1]; int[][] cost = new int[P + 1][Q + 1]; // Starting point (1, 1) queue.add(new int[] {1, 1}); [1][1] = A[1] * B[1]; visited[1][1] = true; // Directions for movement (left, right, up, down) int[] dx = {-1, 1, 0, 0}; int[] dy = {0, 0, -1, 1}; while (!queue.isEmpty()) { int[] point = queue.poll(); int x = point[0]; int y = point[1]; // Explore all four directions for (int i = 0; i < 4; i++) { int nx = x + dx[i]; int ny = y + dy[i]; // Check if the new position is within bounds if (nx >= 1 && nx <= P && ny >= 1 && ny <= Q) { int newCost = A[nx] * B[ny]; // If not visited or found a lower cost path if (!visited[nx][ny] || cost[nx][ny] > cost[x][y] + newCost) { visited[nx][ny] = true; cost[nx][ny] = cost[x][y] + newCost; queue.add(new int[] {nx, ny}); } } } } // Output the minimum cost) System.out.println(cost[P][Q]); } sc.close(); } }
Helper functions:
computeA(int N)
calculates the A[i]
array where for each index i
, it stores max(i, N / i)
if i
divides N
and 0
otherwise.computeB(int M)
calculates the B[i]
array in the same way for M
.Main Functionality:
(1, 1)
to (P, Q)
.cost
array keeps track of the minimum cost to reach each point, and the visited
array ensures we don't revisit points unnecessarily.Edge Cases:
10^8
, we only compute values for relevant positions of A[i]
and B[i]
instead of explicitly constructing large arrays.P x Q
, so the time complexity per test case is approximately O(P * Q)
in the worst case. However, this can be optimized with more advanced techniques like Dijkstra's algorithm or A* if necessary.This solution should work efficiently for reasonable input sizes within the problem's constraints.