C - Maximize GCD Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 600600

問題文

NN 項からなる正整数列 A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N) が与えられます。あなたはこの数列に対して、次の操作を 00 回以上 KK 回以下行うことができます:

  • i{1,2,,N}i\in \{1,2,\ldots,N\} をひとつ選び、AiA_i11 を加える。

操作後の gcd(A1,A2,,AN)\gcd(A_1, A_2, \ldots, A_N) としてありうる最大値を求めてください。

制約

  • 2N3×1052\leq N\leq 3\times 10^5
  • 1K10181\leq K\leq 10^{18}
  • 1Ai3×1051 \leq A_i\leq 3\times 10^5

入力

入力は以下の形式で標準入力から与えられます。

NN KK
A1A_1 A2A_2 \ldots ANA_N

出力

操作後の gcd(A1,A2,,AN)\gcd(A_1, A_2, \ldots, A_N) としてありうる最大値を出力してください。


入力例 1Copy

Copy
3 6
3 4 9

出力例 1Copy

Copy
5

例えば以下のようにして、gcd(A1,A2,A3)=5\gcd(A_1, A_2, A_3) = 5 を実現できます。

  • i=1i = 1 に対して 22 回、i=2i = 2 に対して 11 回、i=3i=3 に対して 11 回の操作を行う。合計の操作回数は 44 回で、K=6K=6 以下である。
  • 操作の結果、A1=5A_1 = 5, A2=5A_2 = 5, A3=10A_3 = 10 となり、gcd(A1,A2,A3)=5\gcd(A_1, A_2, A_3) = 5 である。

入力例 2Copy

Copy
3 4
30 10 20

出力例 2Copy

Copy
10

操作を一度も行わないことで、gcd(A1,A2,A3)=10\gcd(A_1, A_2, A_3) = 10 を実現できます。


入力例 3Copy

Copy
5 12345
1 2 3 4 5

出力例 3Copy

Copy
2472

Score : 600600 points

Problem Statement

Given is a sequence of NN positive integers: A=(A1,A2,,AN)A = (A_1, A_2, \ldots, A_N). You can do the following operation on this sequence at least zero and at most KK times:

  • choose i{1,2,,N}i\in \{1,2,\ldots,N\} and add 11 to AiA_i.

Find the maximum possible value of gcd(A1,A2,,AN)\gcd(A_1, A_2, \ldots, A_N) after your operations.

Constraints

  • 2N3×1052\leq N\leq 3\times 10^5
  • 1K10181\leq K\leq 10^{18}
  • 1Ai3×1051 \leq A_i\leq 3\times 10^5

Input

Input is given from Standard Input in the following format:

NN KK
A1A_1 A2A_2 \ldots ANA_N

Output

Print the maximum possible value of gcd(A1,A2,,AN)\gcd(A_1, A_2, \ldots, A_N) after your operations.


Sample Input 1Copy

Copy
3 6
3 4 9

Sample Output 1Copy

Copy
5

One way to achieve gcd(A1,A2,A3)=5\gcd(A_1, A_2, A_3) = 5 is as follows.

  • Do the operation with i=1i = 1 twice, with i=2i = 2 once, and with i=3i = 3 once, for a total of four times, which is not more than K=6K=6.
  • Now we have A1=5A_1 = 5, A2=5A_2 = 5, A3=10A_3 = 10, for which gcd(A1,A2,A3)=5\gcd(A_1, A_2, A_3) = 5.

Sample Input 2Copy

Copy
3 4
30 10 20

Sample Output 2Copy

Copy
10

Doing no operation achieves gcd(A1,A2,A3)=10\gcd(A_1, A_2, A_3) = 10.


Sample Input 3Copy

Copy
5 12345
1 2 3 4 5

Sample Output 3Copy

Copy
2472


2025-03-29 (Sat)
07:41:40 +00:00