Game of the Year
Contest: Educational Codeforces Round 141 (Rated for Div. 2)
Difficulty rating: 2300
Tags: brute force, data structures, math, number theory

Monocarp and Polycarp are playing a computer game. This game features $$$n$$$ bosses for the playing to kill, numbered from $$$1$$$ to $$$n$$$.

They will fight each boss the following way:

- Monocarp makes $$$k$$$ attempts to kill the boss;
- Polycarp makes $$$k$$$ attempts to kill the boss;
- Monocarp makes $$$k$$$ attempts to kill the boss;
- Polycarp makes $$$k$$$ attempts to kill the boss;
- ...

Monocarp kills the $$$i$$$-th boss on his $$$a_i$$$-th attempt. Polycarp kills the $$$i$$$-th boss on his $$$b_i$$$-th attempt. After one of them kills the $$$i$$$-th boss, they move on to the $$$(i+1)$$$-st boss. The attempt counters reset for both of them. Once one of them kills the $$$n$$$-th boss, the game ends.

Find all values of $$$k$$$ from $$$1$$$ to $$$n$$$ such that Monocarp kills all bosses.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases.

The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of bosses.

The second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le n$$$) — the index of attempt Monocarp kills each boss on.

The third line contains $$$n$$$ integers $$$b_1, b_2, \dots, b_n$$$ ($$$1 \le b_i \le n$$$) — the index of attempt Polycarp kills each boss on.

The sum of $$$n$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$.

Output format:
For each testcase, print two lines. The first line should contain a single integer $$$\mathit{cnt}$$$ — the number of values of $$$k$$$ from $$$1$$$ to $$$n$$$ such that Monocarp kills all bosses. The second line should contain $$$\mathit{cnt}$$$ distinct integers — the values of $$$k$$$ themselves.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
Consider the last testcase of the example.

Let $$$k = 1$$$. First, Monocarp makes one attempt to kill the first boss. It's successful, since $$$a_1 = 1$$$. Then, Monocarp makes one attempt to kill the second boss. It's unsuccessful, since $$$a_2 > 1$$$. So, Polycarp makes an attempt then. It's also unsuccessful, since $$$b_2 > 1$$$. Then, Monocarp makes another attempt. It's still unsuccessful, since $$$a_2 > 2$$$. This goes on until Polycarp finally kills the boss on his third attempt. Monocarp didn't kill this boss, thus, $$$k = 1$$$ isn't the answer.

Let $$$k = 2$$$. Monocarp still kills the first boss on his first attempt. Then, he makes two unsuccessful attempts for the second boss. Then, Polycarp makes two unsuccessful attempts. Then, Monocarp makes two more attempts and kills the boss on his fourth attempt. The third boss is similar. First, two unsuccessful attempts by Monocarp. Then, two unsuccessful attempts by Polycarp. Then, Monocarp has two more attempts, but even his first one is successful, since $$$a_3 = 3$$$. The fourth boss is also killed by Monocarp. Thus, $$$k = 2$$$ is the answer.

Example input:
3
3
1 1 1
2 3 1
1
1
1
4
1 4 3 2
3 3 4 1

Example output:
3
1 2 3 
1
1 
2
2 4

Editorial:
Consider some value of $$$k$$$. When is it included in the answer? When Monocarp spends a lower or an equal amount of "blocks" of attempts than Polycarp for killing every boss.
Formally, $$$\lceil \frac{a_i}{k} \rceil \le \lceil \frac{b_i}{k} \rceil$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$.
Let's reverse this condition. $$$k$$$ is not in the answer if there exists such $$$i$$$ from $$$1$$$ to $$$n$$$ that $$$\lceil \frac{b_i}{k} \rceil < \lceil \frac{a_i}{k} \rceil$$$. So, there exists at least one value between $$$\lceil \frac{b_i}{k} \rceil$$$ and $$$\lceil \frac{a_i}{k} \rceil$$$. Let's call it $$$x$$$. Now it's $$$\lceil \frac{b_i}{k} \rceil < x \le \lceil \frac{a_i}{k} \rceil$$$. I set the $$$\le$$$ and $$$<$$$ signs arbitrarily, just so that it shows that such a value exists. You can't put both $$$\le$$$ or both $$$<$$$, because that will accept $$$0$$$ values or at least $$$2$$$ values, respectively.
Would be cool if we could multiply everything by $$$k$$$ and it still worked. Is it completely impossible, though? Take a look at $$$b_i < xk \le a_i$$$. What it says is that there exists a multiple of $$$k$$$ between $$$b_i$$$ and $$$a_i$$$. A multiple of $$$k$$$ is a number that's the last in each "block" of attempts (the block of value that are rounded up the same). Turns out, this is what we are looking for already. Right after the multiple of $$$k$$$, the new block starts. Thus, we are wrong we our signs. It should be $$$b_i \le xk < a_i$$$ — $$$a_i$$$ is in the block after $$$b_i$$$, so it requires more blocks of attempts.
So for $$$k$$$ to not be included in the answer, there should exist at least one $$$i$$$ such that there exists a multiple of $$$k$$$ in the half-interval $$$[b_i; a_i)$$$.
That is pretty easy to implement. For each $$$x$$$, calculate the number of half-intervals that cover $$$x$$$. I think this is called delta-encoding. Iterate over all half-intervals and make two updates for each one: increment by $$$1$$$ on position $$$b_i$$$ and decrement by $$$1$$$ on position $$$a_i$$$. Then make a prefix sum over these updates. Now the value in the $$$x$$$-th position tells you the number of half-intervals that cover $$$x$$$.
To check a particular value of $$$k$$$, iterate over all multiples of $$$k$$$ and check that none are covered by half-intervals. It's known that the total number of multiples over all numbers from $$$1$$$ to $$$n$$$ is $$$n + \frac{n}{2} + \frac{n}{3} + \dots + \frac{n}{n} = O(n \log n)$$$.
Overall complexity: $$$O(n \log n)$$$ per testcase.