GCD 财宝AI (CI翻译)
The greedy pirate Dimash found a treasury. It consists of $n$ piles of coins, numbered from $1$ to $n$. The $i$-th pile contains exactly $a_i$ coins. Pirate Dimash has a number $x$ that he can use to steal coins from the treasury. The stealing process works as follows:
贪婪的海盗 Dimash 发现了一处财宝。财宝由 $n$ 堆金币组成,编号为 $1$ 到 $n$。第 $i$ 堆恰好有 $a_i$ 枚金币。海盗 Dimash 有一个数 $x$,可以用它从财宝中偷取金币。偷取过程如下:AI (CI翻译)
- First, he chooses an index
$1\le i\le n$such that$a_i\gt 0$and$\gcd. If there is no such index, the pirate stops.$$^{\text{∗}}$$(a_i, x) \neq 1$ - Now let
$\gcd(a_i, x)$be$g$. The pirate steals exactly$g$coins from pile$i$, after which$a_i$decreases by$g$. - Finally, he sets
$x$to$g$and continues stealing coins.
- 首先,他选择一个满足
$1\le i\le n$、$a_i\gt 0$且$\gcd的下标。如果不存在这样的下标,海盗就停止偷取。$$^{\text{∗}}$$(a_i, x) \neq 1$ - 令
$\gcd(a_i, x)$为$g$。海盗从第$i$堆中恰好偷走$g$枚金币,之后$a_i$减少$g$。 - 最后,他将
$x$设为$g$,并继续偷取金币。
Your task is to help the pirate steal the maximum possible number of coins. Find the maximum number of coins that can be stolen from the treasury.
请帮助海盗偷取尽可能多的金币。求他最多能从财宝中偷走多少枚金币。AI (CI翻译)
$^{\text{∗}}$$\gcd(a_i, x)$ denotes the greatest common divisor (GCD) of integers $a_i$ and $x$.
$^{\text{∗}}$$\gcd(a_i, x)$ 表示整数 $a_i$ 和 $x$ 的最大公约数(GCD)。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个输入包含多组测试用例。第一行包含测试用例数量 $t$ ($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)
The first line of each test case contains two integers $n$ and $x$ ($1\le n, x\le 3\cdot 10^5$) — the number of piles of coins in the treasury and the pirate's number.
每个测试用例的第一行包含两个整数 $n$ 和 $x$ ($1\le n, x\le 3\cdot 10^5$)——财宝中金币堆的数量和海盗的数。AI (CI翻译)
The second line of each test case contains $n$ integers $a_1, a_2, \ldots a_n$ ($1 \le a_i \le 3\cdot 10^5$).
每个测试用例的第二行包含 $n$ 个整数 $a_1, a_2, \ldots a_n$ ($1 \le a_i \le 3\cdot 10^5$)。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot 10^5$.
保证所有测试用例的 $n$ 之和不超过 $3\cdot 10^5$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output one number — the maximum number of coins that can be stolen.
对于每个测试用例,输出一个数——海盗最多能偷走的金币数量。AI (CI翻译)
Note
说明
AI (CI翻译)In the first test case, no coin can be stolen.
在第一个测试用例中,海盗无法偷走任何金币。AI (CI翻译)
In the second test case, the pirate steals as follows.
在第二个测试用例中,海盗按如下方式偷取金币:AI (CI翻译)
- The pirate chooses pile number
$3$. He takes$4$coins, after which the treasury becomes$[2, 3, 0]$and$x$becomes$4$. - The pirate chooses pile number
$1$. He takes$2$coins, after which the treasury becomes$[0, 3, 0]$, and$x$becomes$2$. - No suitable indices remain, so the pirate stops. The pirate managed to take
$4 + 2 = 6$coins.
- 海盗选择第
$3$堆,拿走$4$枚金币。此后财宝变为$[2, 3, 0]$,$x$变为$4$。 - 海盗选择第
$1$堆,拿走$2$枚金币。此后财宝变为$[0, 3, 0]$,$x$变为$2$。 - 已经没有符合条件的下标,因此海盗停止。他总共拿走了
$4 + 2 = 6$枚金币。
样例
5
3 1
2 3 5
3 4
2 3 4
4 2
2 2 2 2
6 6
2 3 2 3 2 3
7 6
9 9 4 4 4 4 4
0
6
8
9
20
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。