CF2267C.GCD Treasury
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 ai coins. Pirate Dimash has a number x that he can use to steal coins from the treasury. The stealing process works as follows:
- First, he chooses an index 1≤i≤n such that ai>0 and gcd^{\text{∗}}(ai,x)=1. If there is no such index, the pirate stops.
- Now let gcd(ai,x) be g. The pirate steals exactly g coins from pile i, after which ai decreases by g.
- Finally, he sets x to g and continues stealing coins.
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.
∗\gcd(a_i, x)$ denotes the greatest common divisor (GCD) of integers a_i and x.
贪婪的海盗迪马什发现了一座宝库。宝库由 n 堆金币组成,编号从 1 到 n。第 i 堆恰好包含 ai 枚金币。海盗迪马什拥有一个数 x,他可利用该数从宝库中窃取金币。窃取过程如下:
- 首先,他选择一个下标 1≤i≤n,满足 ai>0 且 gcd^{\text{∗}}(ai,x)=1。若不存在这样的下标,则海盗停止行动。
- 设此时 gcd(ai,x)=g。海盗从第 i 堆中恰好窃取 g 枚金币,此后 ai 减少 g。
- 最后,他将 x 更新为 g,并继续窃取金币。
你的任务是帮助海盗窃取尽可能多的金币。请找出能从宝库中窃取的金币最大总数。
∗\gcd(a_i, x)$ 表示整数 a_i 与 x 的最大公约数(GCD)。
输入格式
Each test contains multiple test cases. The first line contains the number of test cases t (1≤t≤104). The description of the test cases follows.
The first line of each test case contains two integers n and x (1≤n,x≤3⋅105) — the number of piles of coins in the treasury and the pirate's number.
The second line of each test case contains n integers a1,a2,…an (1≤ai≤3⋅105).
It is guaranteed that the sum of n over all test cases does not exceed 3⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含两个整数 n 和 x(1≤n,x≤3⋅105)—— 分别表示国库中金币堆的数量以及海盗的编号。
每个测试用例的第二行包含 n 个整数 a1,a2,…an(1≤ai≤3⋅105)。
保证所有测试用例的 n 之和不超过 3⋅105。
输出格式
For each test case, output one number — the maximum number of coins that can be stolen.
对于每个测试用例,输出一个数字——能够窃取的硬币的最大数量。
输入输出样例
输入#1
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
输出#1
0 6 8 9 20
说明/提示
In the first test case, no coin can be stolen.
In the second test case, the pirate steals as follows.
- 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 枚硬币。
输入解题思路,AI测评打分。不知道怎么写?