#GL0000339. GCD lớn nhất trên lưới (GCD on a Grid)
Bài tập thuộc lộ trình
Mục lục đề ⌄
GCD lớn nhất trên lưới (GCD on a Grid)
GCD on a Grid
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given an grid of positive integers, start at cell and reach using only moves right or down.
For a path, take the GCD of all grid values visited, including both endpoints. Find the maximum GCD attainable over all valid paths.
Each PHOJ file contains one grid, with no initial test count.
Input
The first line contains , followed by lines of positive integers .
Output
Print the maximum possible path GCD.
Subtasks
-
Subtask 1 (20 points): , .
-
Subtask 2 (30 points): , .
-
Subtask 3 (50 points): , .
Examples
Example 1
Input
2 3
30 20 30
15 25 40
Output
10
Explanation
Move right twice and then down: the values 30, 20, 30, 40 have gcd 10. No path yields a greater gcd.
Example 2
Input
3 3
12 4 9
3 12 2
8 3 12
Output
3
Explanation
The path 12→3→12→3→12 has gcd 3. Despite equal endpoints, no full path is divisible by 4, 6, or 12.