最新文章

这里会显示最新的几篇文章摘要。

A. Kamilka and the Sheep(最大公约数)

A. Kamilka and the Sheep

codeforces Round 1014(Div.2)

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Kamilka has a flock of \(n\) sheep, the \(i\)-th of which has a beauty level of \(a_i\). All \(a_i\) are distinct. Morning has come, which means they need to be fed. Kamilka can choose a non-negative integer \(d\) and give each sheep \(d\) bunches of grass. After that, the beauty level of each sheep increases by \(d\).

In the evening, Kamilka must choose exactly two sheep and take them to the mountains. If the beauty levels of these two sheep are \(x\) and \(y\) (after they have been fed), then Kamilka's pleasure from the walk is equal to \(\gcd(x, y)\), where \(\gcd(x, y)\) denotes the greatest common divisor (GCD) of integers \(x\) and \(y\).

The task is to find the maximum possible pleasure that Kamilka can get from the walk.

Input

Each test consists of several test cases. The first line contains one integer \(t\) (\(1 \le t \le 500\)), the number of test cases. The description of the test cases follows.

The first line of each test case contains one integer \(n\) (\(2 \leq n \leq 100\)), the number of sheep Kamilka has.

The second line of each test case contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n \ (1 \le a_i \le 10^9)\) — the beauty levels of the sheep.

It is guaranteed that all \(a_i\) are distinct.

Output

For each test case, output a single integer: the maximum possible pleasure that Kamilka can get from the walk.
Example
InputCopy
4
2
1 3
5
5 4 3 2 1
3
5 6 7
3
1 11 10
OutputCopy
2
4
2
10

Note

In the first test case, \(d=1\) works. In this case, the pleasure is \(\gcd(1+1, \ 1+3)=\gcd(2, \ 4)=2\). It can be shown that a greater answer cannot be obtained.

In the second test case, let's take \(d=3\). In this case, the pleasure is \(\gcd(1+3, \ 5+3)=\gcd(4, \ 8)=4\). Thus, for this test case, the answer is \(4\).

题意

就是给定不同的n个数,找到两个数\(a_i\)\(a_j\),使\(a_i + d\)\(a_j + d\)的最大公约数最大,其中d是一个非负整数,求这个最大公约数

分析

由欧几里得算法\(\gcd(a, b) = \gcd(b, a \mod b)\),那么\(gcd(a + d, b + d) = gcd(a - b, a + d)\),可以看出,选择什么样的d都不影响\(a-b\)的值,那么最大可以选择一个d,使得\((b+d) mod (a-b)= 0\)那么,\(a-b\)就是最大公约数

posted @ 2025-03-30 13:04  bakul  阅读(88)  评论(0)    收藏  举报