[Solution] Lost Arithmetic Progression solution codeforces

Lost Arithmetic Progression solution codeforces – 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.

[Solution] Lost Arithmetic Progression solution codeforces

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−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 109+7109+7.

Input

The first line of input contains a single integer 𝑡t (1𝑡1001≤t≤100) denoting the number of testcases.

The first line of each testcase contains three integers 𝑏b𝑞q and 𝑦y (109𝑏109−109≤b≤1091𝑞1091≤q≤1092𝑦1092≤y≤109) 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 (109𝑐109−109≤c≤1091𝑟1091≤r≤1092𝑧1092≤z≤109) denoting the first term, common difference and number of terms of 𝐶C respectively.

[Solution] Lost Arithmetic Progression solution codeforces

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−1.

Otherwise, print the number of finite arithmetic progressions which could be your lost progression 𝐴A modulo 109+7109+7. In particular, if there are no such finite arithmetic progressions, print 00.

Example
input

Copy
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
output

Copy
0
10
-1
0
-1
21
0
273000

Lost Arithmetic Progression solution codeforces

For the first testcase, 𝐵={3,2,1,0,1,2,3}B={−3,−2,−1,0,1,2,3} and 𝐶={1,1,3,5}C={−1,1,3,5}. There is no such arithmetic progression which can be equal to 𝐴A because 55 is not present in 𝐵B and for any 𝐴A55 should not be present in 𝐶C also.

For the second testcase, 𝐵={9,6,3,0,3,6,9,12,15,18,21}B={−9,−6,−3,0,3,6,9,12,15,18,21} and 𝐶={0,6,12}C={0,6,12}. There are 1010 possible arithmetic progressions which can be 𝐴A:

  • {0,6,12}{0,6,12}
  • {0,2,4,6,8,10,12}{0,2,4,6,8,10,12}
  • {0,2,4,6,8,10,12,14}{0,2,4,6,8,10,12,14}
  • {0,2,4,6,8,10,12,14,16}{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}
  • {2,0,2,4,6,8,10,12,14}{−2,0,2,4,6,8,10,12,14}
  • {2,0,2,4,6,8,10,12,14,16}{−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}
  • {4,2,0,2,4,6,8,10,12,14}{−4,−2,0,2,4,6,8,10,12,14}
  • {4,2,0,2,4,6,8,10,12,14,16}{−4,−2,0,2,4,6,8,10,12,14,16}

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

  • {7,12,17,22}{7,12,17,22}
  • {7,12,17,22,27}{7,12,17,22,27}
  • {7,12,17,22,27,32}{7,12,17,22,27,32}
  • {7,12,17,22,27,32,37}{7,12,17,22,27,32,37}
  • {7,12,17,22,27,32,37,42}{7,12,17,22,27,32,37,42}

 

For Solution

Click here

Leave a Comment