Lost Arithmetic Progression
Contest: Codeforces Round 785 (Div. 2)
Difficulty rating: 1900
Tags: combinatorics, math, number theory

Long ago, you thought of two finite arithmetic progressions $$$A$$$ and $$$B$$$. Then you found out another sequence $$$C$$$ containing all elements common to both $$$A$$$ and $$$B$$$. It is not hard to see that $$$C$$$ is also a finite arithmetic progression. After many years, you forgot what $$$A$$$ was but remember $$$B$$$ and $$$C$$$. You are, for some reason, determined to find this lost arithmetic progression. Before you begin this eternal search, you want to know how many different finite arithmetic progressions exist which can be your lost progression $$$A$$$.

Two arithmetic progressions are considered different if they differ in their first term, common difference or number of terms.

It may be possible that there are infinitely many such progressions, in which case you won't even try to look for them! Print $$$-1$$$ in all such cases.

Even if there are finite number of them, the answer might be very large. So, you are only interested to find the answer modulo $$$10^9+7$$$.

Input format:
The first line of input contains a single integer $$$t$$$ ($$$1\leq t\leq 100$$$) denoting the number of testcases.

The first line of each testcase contains three integers $$$b$$$, $$$q$$$ and $$$y$$$ ($$$-10^9\leq b\leq 10^9$$$, $$$1\leq q\leq 10^9$$$, $$$2\leq y\leq 10^9$$$) denoting the first term, common difference and number of terms of $$$B$$$ respectively.

The second line of each testcase contains three integers $$$c$$$, $$$r$$$ and $$$z$$$ ($$$-10^9\leq c\leq 10^9$$$, $$$1\leq r\leq 10^9$$$, $$$2\leq z\leq 10^9$$$) denoting the first term, common difference and number of terms of $$$C$$$ respectively.

Output format:
For each testcase, print a single line containing a single integer.

If there are infinitely many finite arithmetic progressions which could be your lost progression $$$A$$$, print $$$-1$$$.

Otherwise, print the number of finite arithmetic progressions which could be your lost progression $$$A$$$ modulo $$$10^9+7$$$. In particular, if there are no such finite arithmetic progressions, print $$$0$$$.

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

Note:
For the first testcase, $$$B=\{-3,-2,-1,0,1,2,3\}$$$ and $$$C=\{-1,1,3,5\}$$$. There is no such arithmetic progression which can be equal to $$$A$$$ because $$$5$$$ is not present in $$$B$$$ and for any $$$A$$$, $$$5$$$ should not be present in $$$C$$$ also.

For the second testcase, $$$B=\{-9,-6,-3,0,3,6,9,12,15,18,21\}$$$ and $$$C=\{0,6,12\}$$$. There are $$$10$$$ possible arithmetic progressions which can be $$$A$$$:

- $$$\{0,6,12\}$$$
- $$$\{0,2,4,6,8,10,12\}$$$
- $$$\{0,2,4,6,8,10,12,14\}$$$
- $$$\{0,2,4,6,8,10,12,14,16\}$$$
- $$$\{-2,0,2,4,6,8,10,12\}$$$
- $$$\{-2,0,2,4,6,8,10,12,14\}$$$
- $$$\{-2,0,2,4,6,8,10,12,14,16\}$$$
- $$$\{-4,-2,0,2,4,6,8,10,12\}$$$
- $$$\{-4,-2,0,2,4,6,8,10,12,14\}$$$
- $$$\{-4,-2,0,2,4,6,8,10,12,14,16\}$$$

For the third testcase, $$$B=\{2,7,12,17,22\}$$$ and $$$C=\{7,12,17,22\}$$$. There are infinitely many arithmetic progressions which can be $$$A$$$ like:

- $$$\{7,12,17,22\}$$$
- $$$\{7,12,17,22,27\}$$$
- $$$\{7,12,17,22,27,32\}$$$
- $$$\{7,12,17,22,27,32,37\}$$$
- $$$\{7,12,17,22,27,32,37,42\}$$$
- $$$\ldots$$$

Example input:
8
-3 1 7
-1 2 4
-9 3 11
0 6 3
2 5 5
7 5 4
2 2 11
10 5 3
0 2 9
2 4 3
-11 4 12
1 12 2
-27 4 7
-17 8 2
-8400 420 1000000000
0 4620 10

Example output:
0
10
-1
0
-1
21
0
273000

Editorial:
If all elements of $$$C$$$ are not present in $$$B$$$, then the answer is $$$0$$$. It is sufficient to check the following $$$4$$$ conditions to check if all elements of $$$C$$$ are present in $$$B$$$ or not -
• The first term of $$$B\leq$$$ The first term of $$$C$$$, i.e., $$$b\leq c$$$.
• The last term of $$$B\geq$$$ The last term of $$$C$$$, i.e., $$$b+(y-1)q\geq c+(z-1)r$$$.
• The common difference of $$$C$$$ must be divisible by the common difference of $$$B$$$, i.e., $$$r\bmod q=0$$$.
• The first term of $$$C$$$ must lie in $$$B$$$, i.e., $$$(c-b)\bmod q=0$$$.
Now suppose the following conditions are satisfied. Let's denote an Arithmetic Progression (AP) with first term $$$a$$$, common difference $$$d$$$ and $$$n$$$ number of terms by $$$[a,d,n]$$$.
If $$$b>c-r$$$ then there are infinite number of progressions which can be $$$A$$$ like $$$[c,r,z]$$$, $$$[c-r,r,z+1]$$$, $$$[c-2r,r,z+2]$$$ and so on. Similarly, if $$$b+(y-1)q<c+zr$$$, there are infinite number of progressions which can be $$$A$$$ like $$$[c,r,z]$$$, $$$[c,r,z+1]$$$, $$$[c,r,z+2]$$$ and so on.
Otherwise, there are a finite number of progressions which can be $$$A$$$. Let's count them. Let $$$A$$$ be the AP $$$[a,p,x]$$$ and $$$l=a+(x-1)p$$$. It can be seen that $$$lcm(p,q)=r$$$, $$$(c-a)\bmod p=0$$$, $$$a>c-r$$$ and $$$l<c+rz$$$ for any valid $$$A$$$. The first two conditions are trivial. The third condition is necessary because if $$$a\leq c-r$$$ then $$$c-r$$$ will always be present in both $$$A$$$ and $$$B$$$ contradicting the fact that $$$C$$$ contains all the terms common to $$$A$$$ and $$$B$$$. Similarly, the fourth condition is also necessary.
The only possible values $$$p$$$ can take according to the first condition are factors of $$$r$$$ which can be enumerated in $$$\mathcal{O}(\sqrt{r})$$$. The $$$lcm$$$ condition can be checked in $$$\mathcal{O}(\log r)$$$. For a particular value of $$$p$$$, there are $$$\frac{r}{p}$$$ possible values of $$$a$$$ satisfying conditions 2 and 3 and $$$\frac{r}{p}$$$ possible values of $$$l$$$ satisfying conditions 2 and 4. Thus, the answer is $$$\displaystyle\sum_{lcm(p,q)=r}\Big(\frac{r}{p}\Big)^2$$$.
Time complexity: $$$\mathcal{O}(t\,\sqrt{r}\,\log r)$$$