|
<p>Little Scott recently learned how to perform arithmetic operations modulo some prime number <strong>P</strong>. As a training set he picked two sequences <strong>a</strong> of length <strong>N</strong> and <strong>b</strong> of length <strong>M</strong>, generated in the following way:</p> |
|
|
|
a<sub>1</sub>=A1<br/> |
|
a<sub>2</sub>=A2<br/> |
|
a<sub>i</sub>=(a<sub>i-2</sub> * A3 + a<sub>i-1</sub>*A4 + A5) mod P, for i=3...<strong>N</strong><br/> |
|
b<sub>1</sub>=B1<br/> |
|
b<sub>2</sub>=B2<br/> |
|
b<sub>j</sub>=(b<sub>j-2</sub> * B3 + b<sub>j-1</sub> * B4 + B5) mod P, for j=3...<strong>M</strong><br/> |
|
|
|
<p>Now he wants to find the number of pairs (i, j), where 1 ≤ i ≤ <strong>N</strong> and 1 ≤ j ≤ <strong>M</strong>, such that (a<sub>i</sub> * b<sub>j</sub>) mod <strong>P</strong> < <strong>L</strong>, for given number <strong>L</strong>. He asked you to do the same to help him check his answers.</p> |
|
|
|
<h2>Input</h2> |
|
|
|
<p>The first line of input file consists of a single number <strong>T</strong>, the number of test cases. Each test consists of three lines. The first line of a test case contains two integers: prime number <strong>P</strong> and positive integer <strong>L</strong>. The second line consists of six non-negative integers <strong>N</strong>, <strong>A1</strong>, <strong>A2</strong>, <strong>A3</strong>, <strong>A4</strong>, <strong>A5</strong>. Likewise, the third line contains six non-negative integers <strong>M</strong>, <strong>B1</strong>, <strong>B2</strong>, <strong>B3</strong>, <strong>B4</strong>, <strong>B5</strong>. |
|
|
|
<h2>Output</h2> |
|
|
|
<p>Output <strong>T</strong> lines, with the answer to each test case on a single line.</p> |
|
|
|
<h2>Constraints</h2> |
|
<p> |
|
<strong>T</strong> = 20<br/> |
|
2 ≤ <strong>P</strong> < 250,000 <br/> |
|
<strong>P</strong> is prime <br/> |
|
1 ≤ <strong>L</strong> ≤ <strong>P</strong> <br/> |
|
2 ≤ <strong>N</strong>, <strong>M</strong> ≤ 10,000,000 <br/> |
|
0 ≤ <strong>A1</strong>, <strong>A2</strong>, <strong>A3</strong>, <strong>A4</strong>, <strong>A5</strong>, <strong>B1</strong>, <strong>B2</strong>, <strong>B3</strong>, <strong>B4</strong>, <strong>B5</strong> < <strong>P</strong> <br/> |
|
</p> |
|
|