CF2154C2.No Cost Too Great (Hard Version)

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

This is the hard version of the problem. The difference between the versions is that in this version, 1≤bi≤1091 \le b_i \le 10^9 for all ii (1≤i≤n1 \le i \le n). You can hack only if you solved all versions of this problem.

You find yourself with two arrays of positive integers aa and bb, both of length nn. You will perform the following operation any number of times (possibly none):

  • select an integer ii (1≤i≤n1 \le i \le n) and increase aia_i by 11. This has a cost of bib_i.

Determine the minimum total cost to make it so that there exists two integers i,ji, j where 1≤i<j≤n1 \le i \lt j \le n and gcd⁡(ai,aj)\gcd(a_i, a_j)∗^{\text{∗}}$ \gt 1$.

∗^{\text{∗}}gcd⁡(x,y)\gcd(x, y) denotes the greatest common divisor (GCD) of integers xx and yy.

这是该问题的困难版本。两个版本的区别在于,在本版本中,对所有 ii(1≤i≤n1 \le i \le n),均有 1≤bi≤1091 \le b_i \le 10^9。仅当您已解决该问题的所有版本时,才可进行 hack。

您手上有两个长度均为 nn 的正整数数组 aa 和 bb。您可以执行以下操作任意次(包括零次):

  • 选择一个整数 ii(1≤i≤n1 \le i \le n),并将 aia_i 增加 11。该操作的代价为 bib_i。

请确定使下述条件成立所需的最小总代价:存在两个整数 i,ji, j,满足 1≤i<j≤n1 \le i \lt j \le n,且 gcd⁡(ai,aj)\gcd(a_i, a_j)∗^{\text{∗}}$ \gt 1$。

∗^{\text{∗}}gcd⁡(x,y)\gcd(x, y) 表示整数 xx 与 yy 的最大公约数(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 testcase contains an integer nn (2≤n≤2⋅1052 \le n \le 2 \cdot 10^5) — the length of the array aa.

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

The third line of each testcase contains nn integers b1,b2,…,bnb_1,b_2,\ldots,b_n (1≤bi≤109\color{red}{1 \le b_i \le 10^9}).

The sum of nn across all testcases does not exceed 2⋅1052 \cdot 10^5.

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

每个测试用例的第一行包含一个整数 nn(2≤n≤2⋅1052 \le n \le 2 \cdot 10^5)—— 表示数组 aa 的长度。

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

每个测试用例的第三行包含 nn 个整数 b1,b2,…,bnb_1,b_2,\ldots,b_n(1≤bi≤109\color{red}{1 \le b_i \le 10^9})。

所有测试用例中 nn 的总和不超过 2⋅1052 \cdot 10^5。

输出格式

For each testcase, output the minimum cost.

对于每个测试用例,输出最小花费。

输入输出样例

  • 输入#1

    6
    2
    1 1
    1 2
    2
    4 8
    41 67
    5
    1 1 727 1 1
    1 1 1000 1 1
    2
    3 11
    1 1
    3
    2 7 11
    1 6 6
    3
    2 7 11
    100 1 2

    输出#1

    3
    0
    2
    1
    5
    1

说明/提示

In the first testcase we can do the following: [1,1]→x=1[2,1]→x=2[2,2][\color{red}1, 1] \xrightarrow{x = 1} [2, \color{red}1] \xrightarrow{x = 2} [2, 2], this has cost 1+2=31 + 2 = 3. Now gcd⁡(a1,a2)=gcd⁡(2,2)=2\gcd(a_1, a_2) = \gcd(2, 2) = 2 and so gcd⁡(a1,a2)>1\gcd(a_1, a_2) \gt 1. It can be proven that this is the minimum cost required.

In the second testcase it is already true that gcd⁡(a1,a2)=4\gcd(a_1, a_2) = 4 and so gcd⁡(a1,a2)>1\gcd(a_1, a_2) \gt 1. So no operations are required.

在第一个测试用例中,我们可以执行以下操作:[1,1]→x=1[2,1]→x=2[2,2][\color{red}1, 1] \xrightarrow{x = 1} [2, \color{red}1] \xrightarrow{x = 2} [2, 2],总代价为 1+2=31 + 2 = 3。此时 gcd⁡(a1,a2)=gcd⁡(2,2)=2\gcd(a_1, a_2) = \gcd(2, 2) = 2,因此 gcd⁡(a1,a2)>1\gcd(a_1, a_2) \gt 1。可以证明这是所需的最小代价。

在第二个测试用例中,已有 gcd⁡(a1,a2)=4\gcd(a_1, a_2) = 4,因此 gcd⁡(a1,a2)>1\gcd(a_1, a_2) \gt 1,无需执行任何操作。

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

首页