Codeforces Round 1089 (Div. 2)
C2
A Simple GCD Problem (Hard Version)
Codeforces
2100
原题
← C1D →
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version, $1 \leq n \leq 5 \cdot 10^{4}$, $1 \leq b_i \leq 10^{9}$ for $1 \leq i \leq n$, and the time limit is $6$ seconds. Note that a solution for one version does not necessarily solve the other version. You can hack only if you solved all versions of this problem.

翻译中…

You are given two arrays $a$ and $b$ of length $n$.

翻译中…

For each index $i$ ($1 \le i \le n$) of array $a$, you can perform the following operation at most once:

翻译中…
  • choose an arbitrary integer $m$ ($\mathbf{m \neq a_i}$) such that $1 \leq m \leq b_i$, and set $a_i := m$.
翻译中…

Let the array after performing all the operations be $a'$. You can only perform operations in such a way that the following condition holds:

翻译中…
  • For all $1\leq l \lt r\leq n$, $\operatorname{gcd}(a_l,a_{l+1},\ldots,{a_r})=\operatorname{gcd}(a'_l,a'_{l+1},\ldots,a'_r)$
翻译中…

Here, $\gcd(x, y)$ denotes the greatest common divisor (GCD) of integers $x$ and $y$.

翻译中…

You have to determine the maximum number of operations that can be performed while ensuring that the condition remains satisfied.

翻译中…

Input

翻译中…

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

翻译中…

The first line of each test case contains an integer $n$ ($2 \leq n \leq 5\cdot 10^4$) — the length of $a$.

翻译中…

The following line of each test case contains $n$ space-separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).

翻译中…

The next line of each test case contains $n$ space-separated integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$).

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5\cdot 10^4$.

翻译中…

Output

翻译中…

For each test case, output the maximum number of operations which can be done.

翻译中…

Note

翻译中…

In the first test, since the gcd of all subarrays is $1$, we can change the array to $a' = [67, 5, 7, 11, 1, 2, 1]$. Here we have performed $7$ operations, and since there are only $7$ elements in the array, it is the maximum possible.

翻译中…

In the second test case, we can change the array to $a' = [938, 2613, 4489]$. It can be shown that after this transformation, gcd of all subarrays remains the same. Hence the answer is $3$ here.

翻译中…

In the third test case, it can be shown that no operation can be performed, hence the answer is $0$.

翻译中…

样例

Input #1
9
7
1 2 3 4 5 6 7
100 6 12 20 5 2 7
3
67 67 67
1000000000 1000000000 1000000000
6
8 10 10 12 12 14
1 1 1 1 1 1
8
2010 330 550 2 210 385 1001 323
2010 1000 1200 30 500 1000 2000 1000
4
1 2 4 3
1 1 1 1
6
2 3 4 5 1 6
10 15 20 25 5 30
2
1 2
2 100
5
2860 2860 143 9009 9009
2860 2860 1430 9009 9009
3
2 60 60
10 60 60
Output #1
7
3
0
7
1
6
2
0
0
InputCopy OutputCopy
题目信息
题号
2210C2
时间限制
6 s
内存限制
512 MB
标签
动态规划
贪心
数论

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。