#P16343. [科大国创杯初中组 2026] 行走

    ID: 18425 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>动态规划 DP数论安徽最大公约数 gcd2026科创活动初中活动科大国创杯

[科大国创杯初中组 2026] 行走

Background

Subtask 0 uses community testdata, and Subtask 1 uses official testdata.

Problem Description

Xiaokeke has an n×nn \times n grid. Cell (i,j)(i, j) contains a positive integer ai,ja_{i,j}. Xiaokeke wants to walk from (1,1)(1, 1) to (n,n)(n, n). He can only move down or right, that is, from (i,j)(i, j) to (i+1,j)(i+1, j) or from (i,j)(i, j) to (i,j+1)(i, j+1). He has a positive integer vv, initially a1,1a_{1,1}. Each time Xiaokeke arrives at a cell (i,j)(i, j), vv becomes gcd(v,ai,j)\gcd(v, a_{i,j}). Xiaokeke wants to know the maximum possible value of vv when he reaches (n,n)(n, n).

gcd(i,j)\gcd(i, j) denotes the greatest common divisor of positive integers ii and jj, i.e., the largest positive integer dd such that dd divides ii and dd divides jj.

Input Format

The input has a total of n+1n+1 lines.

  • The first line contains two positive integers n,Vn, V.
  • Lines 22 to n+1n+1 each contain nn positive integers. The jj-th number on line i+1i+1 represents ai,ja_{i,j}.

Output Format

Output one line containing one positive integer, representing the answer.

2 20
15 16
12 9
3

Hint

Sample Explanation

Xiaokeke’s optimal route is (1,1)(2,1)(2,2)(1, 1) \to (2, 1) \to (2, 2).

Other Sample Notes

  • Samples 2 to 5: see walk/walk*.in and walk/walk*.ans in the contestant directory.

Constraints

For all data, it is guaranteed that:

  • 1n10001 \le n \le 1000.
  • 1ai,jV100001 \le a_{i,j} \le V \le 10000.
  • All input numbers are positive integers.
Test Point ID nn \le ai,j,Va_{i,j}, V \le Special Property
1,2,31, 2, 3 1010 1000010000 None
4,5,64, 5, 6 100100
7,87, 8 10001000 1000010000 The data is guaranteed to be random
9,109, 10 None

Translated by ChatGPT 5