#P15552. [CCPC 2025 哈尔滨站] k-子集和最大公约数问题

[CCPC 2025 哈尔滨站] k-子集和最大公约数问题

Problem Description

Consider an infinite multiset SS, whose elements include a1,a2,,ana_1, a_2, \cdots, a_n, and each type of element appears infinitely many times. Based on this, we define a function f(k)f(k) as follows: for every subset SS' of SS with size exactly kk, compute the sum of elements in SS'. Take the greatest common divisor of all such sums, and use it as the value of f(k)f(k). Formally,

$$f(k) = \gcd_{S' \subseteq S, |S'| = k} \left( \sum_{x \in S'} x \right)$$

For example, for a=[3,6]a = [3, 6], we have

f(2)=gcd(3+3,3+6,6+6)=3f(2) = \gcd(3 + 3, 3 + 6, 6 + 6) = 3

Now please find the maximum value of f(k)f(k), and the smallest kk that achieves this maximum. In particular, if the maximum value does not exist, report infinite.

Input Format

This problem contains multiple test cases. The first line contains an integer TT (1T4×1051 \le T \le 4 \times 10^5), representing the number of test cases.

Then each test case is given as follows:

The first line contains an integer nn (1n1051 \le n \le 10^5), representing the number of distinct values in SS.

The second line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n (1ai10181 \le a_i \le 10^{18}), representing the elements in SS.

It is guaranteed that the sum of nn over all test cases does not exceed 4×1054 \times 10^5.

Output Format

For each test case, if f(k)f(k) has a maximum value, output one line with two integers: the maximum value of f(k)f(k), and the smallest kk that achieves this maximum.

Otherwise output infinite (without quotes).

2
2
3 6
2
2 2
3 1
infinite
2
3
1 4 7
4
4 16 28 34
3 3
6 3

Hint

For the first test case in Sample 1, you can find that no matter what kk is, f(k)=3f(k)=3.

For the second test case in Sample 1, we have f(k)=2kf(k)=2k, so ff grows without bound and the maximum value does not exist.

Translated by ChatGPT 5