CF2267C.GCD Treasury

普及-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The greedy pirate Dimash found a treasury. It consists of nn piles of coins, numbered from 11 to nn. The ii-th pile contains exactly aia_i coins. Pirate Dimash has a number xx that he can use to steal coins from the treasury. The stealing process works as follows:

  • First, he chooses an index 1≤i≤n1\le i\le n such that ai>0a_i\gt 0 and gcd⁡\gcd^{\text{∗}}(ai,x)≠1(a_i, x) \neq 1. If there is no such index, the pirate stops.
  • Now let gcd⁡(ai,x)\gcd(a_i, x) be gg. The pirate steals exactly gg coins from pile ii, after which aia_i decreases by gg.
  • Finally, he sets xx to gg 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.

∗^{\text{∗}}\gcd(a_i, x)$ denotes the greatest common divisor (GCD) of integers a_ia\_i and xx.

贪婪的海盗迪马什发现了一座宝库。宝库由 nn 堆金币组成,编号从 11 到 nn。第 ii 堆恰好包含 aia_i 枚金币。海盗迪马什拥有一个数 xx,他可利用该数从宝库中窃取金币。窃取过程如下:

  • 首先,他选择一个下标 1≤i≤n1\le i\le n,满足 ai>0a_i > 0 且 gcd⁡\gcd^{\text{∗}}(ai,x)≠1(a_i, x) \neq 1。若不存在这样的下标,则海盗停止行动。
  • 设此时 gcd⁡(ai,x)=g\gcd(a_i, x) = g。海盗从第 ii 堆中恰好窃取 gg 枚金币,此后 aia_i 减少 gg。
  • 最后,他将 xx 更新为 gg,并继续窃取金币。

你的任务是帮助海盗窃取尽可能多的金币。请找出能从宝库中窃取的金币最大总数。

∗^{\text{∗}}\gcd(a_i, x)$ 表示整数 a_ia\_i 与 xx 的最大公约数(GCD)。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and xx (1≤n,x≤3⋅1051\le n, x\le 3\cdot 10^5) — the number of piles of coins in the treasury and the pirate's number.

The second line of each test case contains nn integers a1,a2,…ana_1, a_2, \ldots a_n (1≤ai≤3⋅1051 \le a_i \le 3\cdot 10^5).

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053\cdot 10^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含两个整数 nn 和 xx(1≤n,x≤3⋅1051\le n, x\le 3\cdot 10^5)—— 分别表示国库中金币堆的数量以及海盗的编号。

每个测试用例的第二行包含 nn 个整数 a1,a2,…ana_1, a_2, \ldots a_n(1≤ai≤3⋅1051 \le a_i \le 3\cdot 10^5)。

保证所有测试用例的 nn 之和不超过 3⋅1053\cdot 10^5。

输出格式

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 33. He takes 44 coins, after which the treasury becomes [2,3,0][2, 3, 0] and xx becomes 44.
  • The pirate chooses pile number 11. He takes 22 coins, after which the treasury becomes [0,3,0][0, 3, 0], and xx becomes 22.
  • No suitable indices remain, so the pirate stops. The pirate managed to take 4+2=64 + 2 = 6 coins.

在第一个测试用例中,无法窃取任何硬币。

在第二个测试用例中,海盗按如下方式窃取硬币:

  • 海盗选择第 33 堆。他取走 44 枚硬币,之后金库变为 [2,3,0][2, 3, 0],且 xx 变为 44。
  • 海盗选择第 11 堆。他取走 22 枚硬币,之后金库变为 [0,3,0][0, 3, 0],且 xx 变为 22。
  • 此时已无合适的下标可选,因此海盗停止行动。海盗共窃得 4+2=64 + 2 = 6 枚硬币。

输入解题思路,AI测评打分。不知道怎么写?

首页