Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2014 /round2 /magic_pairs.html
wjomlex's picture
2014 Problems
718fd53 verified
raw
history blame
3.97 kB
<p>
The princess of the kingdom of Hackadia has been kidnapped by an evil dragon. As always the prince Z.A.Y. is going to try to rescue her. The evil dragon is keeping the princess prisoner in his deepest dungeon, and the prince has to solve a puzzle to get her out safely.
</p>
<p>
There are two straight boards in front of the dungeon, both divided into a large number of sections. Each section contains a sparkling gemstone. Each stone has a color. We will denote these colors with numbers.
</p>
<p>
Let's say these boards <strong>Board<sub>1</sub></strong> and <strong>Board<sub>2</sub></strong> contain <strong>N</strong> and <strong>M</strong> sections respectively. Let's call a pair of integers <strong>x</strong>, <strong>y</strong> a magic pair if they have the following properties:
<ol>
<li>0 &le; <strong>x</strong> &lt; <strong>N</strong></li>
<li>0 &le; <strong>y</strong> &lt; <strong>M</strong></li>
<li>The set of different colors in <strong>Board<sub>1</sub></strong>[0...<strong>x</strong>] equals the set of different colors in <strong>Board<sub>2</sub></strong>[0...<strong>y</strong>]</li>
</ol>
</p>
<p>
The prince has asked you to find out how many magic pairs exist for the given two boards, so he can free the princess and become the hero. He will take all the glory from this, so you will have to make do with points in this competition as payment for your help.
</p>
<p>
Since the numbers <strong>N</strong> and <strong>M</strong> might be very large, the colors of the gemstones will be supplied through a pseudo random generator. This works as follows:<br />
<strong>Board<sub>1</sub></strong>[0] = <strong>x1</strong><br />
<strong>Board<sub>2</sub></strong>[0] = <strong>x2</strong><br/>
<strong>Board<sub>1</sub></strong>[<strong>i</strong>] = (<strong>a1</strong> * <strong>Board<sub>1</sub></strong>[(<strong>i</strong>-1) % <strong>N</strong>] + <strong>b1</strong> * <strong>Board<sub>2</sub></strong>[(<strong>i</strong>-1) % <strong>M</strong>] + <strong>c1</strong>) % <strong>r1</strong>, for 0 &lt; <strong>i</strong> &lt; <strong>N</strong><br />
<strong>Board<sub>2</sub></strong>[<strong>i</strong>] = (<strong>a2</strong> * <strong>Board<sub>1</sub></strong>[(<strong>i</strong>-1) % <strong>N</strong>] + <strong>b2</strong> * <strong>Board<sub>2</sub></strong>[(<strong>i</strong>-1) % <strong>M</strong>] + <strong>c2</strong>) % <strong>r2</strong>, for 0 &lt; <strong>i</strong> &lt; <strong>M</strong><br />
</p>
<h3>Input</h3>
<p>
The first line of the input consists of a single integer <strong>T</strong>, the number of test
cases. <br />
Each test case starts with a line containing the integers <strong>N</strong>, <strong>M</strong> <br />
The second line of each test case contains five integers <strong>x1</strong>, <strong>a1</strong>, <strong>b1</strong>, <strong>c1</strong>, <strong>r1</strong><br/>
The third line of each test case contains five integers <strong>x2</strong>, <strong>a2</strong>, <strong>b2</strong>, <strong>c2</strong>, <strong>r2</strong><br />
</p>
<h3>Output</h3>
<p>
For each test case <strong>i</strong> numbered from 1 to <strong>T</strong>, output "Case #<strong>i</strong>: ", followed by the number of magic pairs for the two boards.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 20 <br />
1 &le; <strong>N</strong>, <strong>M</strong> &le; 10^6<br />
0 &le; <strong>x1</strong>, <strong>x2</strong>, <strong>a1</strong>, <strong>a2</strong>, <strong>b1</strong>, <strong>b2</strong>, <strong>c1</strong>, <strong>c2</strong> &le; 10^9<br />
1 &le; <strong>r1</strong>, <strong>r2</strong> &le; 10^9 <br />
</p>
<h3>Examples</h3>
The first example produces the following boards:<br />
<strong>Board<sub>1</sub></strong> = [0, 3, 2, 0, 4, 2, 1, 3]<br />
<strong>Board<sub>2</sub></strong> = [0, 4, 2, 1, 4, 3]<br />
There are 3 magic pairs:<br />
pair(0, 0) ==&gt; (0)<br>
pair(6, 5) ==&gt; (0, 1, 2, 3, 4)<br>
pair(7, 5) ==&gt; (0, 1, 2, 3, 4)<br>