CF1806F2.GCD Master (hard version)
省选/NOI-
通过率:0%
时间限制:3.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The only difference between the two versions is the constraint on m. You can make hacks only if both versions of the problem are solved.
You are given an array a of length n and two integers m and k. Each element in a satisfies 1≤ai≤m.
In one operation, you choose two indices i and j such that 1≤i<j≤∣a∣, then append gcd(ai,aj) to the back of the array and delete ai and aj from the array. Note that the length of the array decreases by one after this operation.
Find the maximum possible sum of the array after performing exactly k operations.
这是该问题的困难版本。两个版本的唯一区别在于对 m 的约束条件。仅当两个版本的问题均被解决时,你才可以进行 hack。
给定一个长度为 n 的数组 a 以及两个整数 m 和 k。数组 a 中的每个元素均满足 1≤ai≤m。
在一次操作中,你需要选择两个下标 i 和 j,满足 1≤i<j≤∣a∣,然后将 gcd(ai,aj) 追加到数组末尾,并从数组中删除 ai 和 aj。注意:执行该操作后,数组长度减少 1。
求恰好执行 k 次操作后,数组元素之和的最大可能值。
输入格式
The first line contains a single integer t (1≤t≤105) — the number of test cases. The description of test cases follows.
The first line of each test case contains three integers n, m and k (2≤n≤106; 1≤m≤9⋅1018; 1≤k≤n−1).
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤m).
It is guaranteed that the sum of n over all test cases does not exceed 106.
第一行包含一个整数 t(1≤t≤105)—— 表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含三个整数 n、m 和 k(2≤n≤106;1≤m≤9⋅1018;1≤k≤n−1)。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤m)。
保证所有测试用例的 n 之和不超过 106。
输出格式
For each test case, output the maximum possible sum of the array after performing k operations optimally.
对于每个测试用例,输出执行 k 次操作后数组可能达到的最大和。
输入输出样例
输入#1
4 3 8 1 4 7 8 5 114514 2 7 2 4 1 6 3 1919810 2 2 3 3 3 9000000000000000000 1 9000000000000000000 9000000000000000000 9000000000000000000
输出#1
11 14 1 18000000000000000000
说明/提示
In the first test case, the best way is to choose i=1, j=3 in the first operation. The final sequence is [7,4].
在第一个测试用例中,最优方案是在第一次操作中选择 i=1、j=3。最终序列为 [7,4]。
输入解题思路,AI测评打分。不知道怎么写?