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≤109 for all i (1≤i≤n). You can hack only if you solved all versions of this problem.
You find yourself with two arrays of positive integers a and b, both of length n. You will perform the following operation any number of times (possibly none):
- select an integer i (1≤i≤n) and increase ai by 1. This has a cost of bi.
Determine the minimum total cost to make it so that there exists two integers i,j where 1≤i<j≤n and gcd(ai,aj)∗$ \gt 1$.
∗gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y.
这是该问题的困难版本。两个版本的区别在于,在本版本中,对所有 i(1≤i≤n),均有 1≤bi≤109。仅当您已解决该问题的所有版本时,才可进行 hack。
您手上有两个长度均为 n 的正整数数组 a 和 b。您可以执行以下操作任意次(包括零次):
- 选择一个整数 i(1≤i≤n),并将 ai 增加 1。该操作的代价为 bi。
请确定使下述条件成立所需的最小总代价:存在两个整数 i,j,满足 1≤i<j≤n,且 gcd(ai,aj)∗$ \gt 1$。
∗gcd(x,y) 表示整数 x 与 y 的最大公约数(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 testcase contains an integer n (2≤n≤2⋅105) — the length of the array a.
The second line of each testcase contains n integers a1,a2,…,an (1≤ai≤2⋅105).
The third line of each testcase contains n integers b1,b2,…,bn (1≤bi≤109).
The sum of n across all testcases does not exceed 2⋅105.
每个测试包含多个测试用例。第一行包含测试用例的数量 t(1≤t≤104)。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(2≤n≤2⋅105)—— 表示数组 a 的长度。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤2⋅105)。
每个测试用例的第三行包含 n 个整数 b1,b2,…,bn(1≤bi≤109)。
所有测试用例中 n 的总和不超过 2⋅105。
输出格式
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], this has cost 1+2=3. Now gcd(a1,a2)=gcd(2,2)=2 and so gcd(a1,a2)>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 and so gcd(a1,a2)>1. So no operations are required.
在第一个测试用例中,我们可以执行以下操作:[1,1]x=1[2,1]x=2[2,2],总代价为 1+2=3。此时 gcd(a1,a2)=gcd(2,2)=2,因此 gcd(a1,a2)>1。可以证明这是所需的最小代价。
在第二个测试用例中,已有 gcd(a1,a2)=4,因此 gcd(a1,a2)>1,无需执行任何操作。
输入解题思路,AI测评打分。不知道怎么写?