luogu#P5020. [NOIP 2018 提高组] 货币系统

    ID: 4031 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>动态规划 DP数学贪心2018NOIP 提高组背包 DP

[NOIP 2018 提高组] 货币系统

Background

NOIP 2018 Senior D1T2.

Problem Description

In the netizens’ kingdom, there are nn different denominations of currency. The denomination of the ii-th currency is a[i]a[i]. You may assume that there are infinitely many bills of each denomination. For convenience, we denote the currency system with nn types of currency and denomination array a[1..n]a[1..n] as (n,a)(n,a).

In a perfect currency system, every non-negative integer amount xx should be representable. That is, for every non-negative integer xx, there exist nn non-negative integers t[i]t[i] such that the sum of a[i]×t[i]a[i] \times t[i] equals xx. However, in the netizens’ kingdom, the currency system may be imperfect, meaning there may exist an amount xx that cannot be represented by the system. For example, in the currency system n=3n=3, a=[2,5,9]a=[2,5,9], the amounts 1,31,3 cannot be represented.

Two currency systems (n,a)(n,a) and (m,b)(m,b) are equivalent if and only if, for any non-negative integer xx, either xx can be represented by both systems, or xx cannot be represented by either of them.

Now the netizens plan to simplify the currency system. They want to find a currency system (m,b)(m,b) such that (m,b)(m,b) is equivalent to the original currency system (n,a)(n,a), and mm is as small as possible. They hope you can help complete this difficult task: find the minimum mm.

Input Format

The first line of the input file contains an integer TT, the number of test cases.

Then TT test cases are given in the following format. The first line of each test case contains a positive integer nn. The next line contains nn positive integers a[i]a[i] separated by spaces.

Output Format

The output file contains TT lines. For each test case, output one positive integer per line, representing the minimum mm among all currency systems (m,b)(m,b) that are equivalent to (n,a)(n,a).

2
4
3 19 10 6
5
11 29 13 19 17
2
5

Hint

In the first test case, the currency system (2,[3,10])(2, [3,10]) is equivalent to the given currency system (n,a)(n, a). It can be verified that there is no equivalent currency system with m<2m < 2, so the answer is 22. In the second test case, it can be verified that there is no equivalent currency system with m<nm < n, so the answer is 55.

【Constraints】

For 100%100\% of the testdata, 1T201 \le T \le 20, and n,a[i]1n,a[i] \ge 1.

Translated by ChatGPT 5