Datasets:

Modalities:
Image
Text
Formats:
parquet
Size:
< 1K
Tags:
code
Libraries:
Datasets
pandas
License:
hackercup / 2017 /round2 /fightingallthezombies.html
wjomlex's picture
2017 Problems
7acee6b verified
raw
history blame
7.01 kB
<p>
At last, your power knows no bounds!
Within the world of the ever-popular role-playing game <em>Dungeons & Dragons</em>, or <em>D&D</em>,
you've managed to become one of the most powerful wizards to have ever wizarded.
</p>
<p>
And yet, you're still not content &mdash; you feel compelled to continue grinding away, repeatedly destroying monsters to ever so slowly increase your power further.
There's a particular cave which contains <strong>N</strong> zombies, and these zombies respawn every time you reenter the cave.
As such, you can efficiently gain experience by entering, destroying all of them, and leaving, repeating this process infinitely (or until your supply of Hot Pockets runs out).
The <i>n</i>th zombie has a power level of <i>n</i>.
</p>
<p>
Conveniently, you're also carrying <strong>N</strong> wands with you, the <i>n</i>th of which has a power level of <i>n</i>.
Each time you enter the cave, you'll use each of your wands exactly once to cast a spell and destroy a single zombie, such that all <strong>N</strong> zombies are defeated.
Initially, you know <strong>N</strong> different spells, the <i>n</i>th of which allows your wand with power level <i>n</i> to target the zombie with power level <i>n</i>.
</p>
<p>
You're going to visit the cave <strong>M</strong> times today.
Before each visit, you're going to learn some new spells (which you'll continue to be able to use for all of your remaining visits).
In particular, before your <i>i</i>th visit, you'll acquire <strong>S<sub>i</sub></strong> different new spells, each of which allows your wand with power level <strong>W<sub>i</sub></strong> to target the zombie with power level of <strong>Z<sub>i</sub></strong>.
Wands are unable to affect zombies which are much more powerful than them, and it would be a waste to use a wand on a zombie much weaker than it.
As such, |<strong>W<sub>i</sub></strong> - <strong>Z<sub>i</sub></strong>| &le; 1 holds for all spells.
Note that all spells are considered distinct, even if several of them allow a particular wand to target a particular zombie.
</p>
<p>
On each of your <strong>M</strong> visits to the cave, you're interested in the number of different ways in which you can defeat all <strong>N</strong> zombies, using only the spells that you've learned up to that point. Two ways are considered different if at least one spell is used in one of them but not the other. These quantities can get rather large, and you don't want to deal with <strong>M</strong> big numbers, so you only care about computing their sum modulo 1,000,000,007.
</p>
<p>
You're given <strong>W<sub>1</sub></strong>, and <strong>W<sub>2..M</sub></strong> may then be calculated as follows,
using given constants <strong>A<sub>w</sub></strong> and <strong>B<sub>w</sub></strong>:
</p>
<p>
<strong>W<sub>i</sub></strong> =
((<strong>A<sub>w</sub></strong> * <strong>W<sub>i-1</sub></strong> + <strong>B<sub>w</sub></strong>) %
<strong>N</strong>) + 1
</p>
<p>
A temporary sequence of values <strong>D<sub>1..M</sub></strong> will be used to help calculate <strong>Z<sub>1..M</sub></strong>. You're given <strong>D<sub>1</sub></strong>, and <strong>D<sub>2..M</sub></strong> may then be calculated as follows,
using given constants <strong>A<sub>d</sub></strong> and <strong>B<sub>d</sub></strong>:
</p>
<p>
<strong>D<sub>i</sub></strong> =
(<strong>A<sub>d</sub></strong> * <strong>D<sub>i-1</sub></strong> + <strong>B<sub>d</sub></strong>) % 3
</p>
<p>
From these values, <strong>Z<sub>1..M</sub></strong> may then be calculated as follows:
</p>
<p>
<strong>Z<sub>i</sub></strong> = max{ 1, min { <strong>N</strong>, <strong>W<sub>i</sub></strong> +
<strong>D<sub>i</sub></strong> - 1} }
</p>
<p>
Finally, you're given <strong>S<sub>1</sub></strong>, and <strong>S<sub>2..M</sub></strong> may then be calculated as follows,
using given constants <strong>A<sub>s</sub></strong> and <strong>B<sub>s</sub></strong>:
</p>
<p>
<strong>S<sub>i</sub></strong> =
((<strong>A<sub>s</sub></strong> * <strong>S<sub>i-1</sub></strong> + <strong>B<sub>s</sub></strong>) % 1,000,000,000) + 1
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of different caves you'll visit.
For each cave, there are four lines. The first line contains the two space-separated integers
<strong>N</strong> and <strong>M</strong>.
The second line contains the three space-separated integers
<strong>W<sub>1</sub></strong>, <strong>A<sub>w</sub></strong>, and <strong>B<sub>w</sub></strong>.
The third line contains the three space-separated integers
<strong>D<sub>1</sub></strong>, <strong>A<sub>d</sub></strong>, and <strong>B<sub>d</sub></strong>.
The fourth line contains the three space-separated integers
<strong>S<sub>1</sub></strong>, <strong>A<sub>s</sub></strong>, and <strong>B<sub>s</sub></strong>.
</p>
<h3>Output</h3>
<p>
For the <em>i</em>th cave, print a line containing "Case #<strong>i</strong>: " followed by one integer.
This number is the sum (taken modulo 1,000,000,007) of <strong>M</strong> values, the <em>j</em>th of which is the number of
different ways in which you can defeat the zombies on your <em>j</em>th visit to the cave.
</p>
<h3>Constraints</h3>
<p>
1 &le; <strong>T</strong> &le; 20 <br />
1 &le; <strong>N</strong>, <strong>M</strong> &le; 800,000 <br />
1 &le; <strong>W<sub>1</sub></strong> &le; <strong>N</strong> <br />
0 &le; <strong>A<sub>w</sub></strong>, <strong>B<sub>w</sub></strong> &le; <strong>N</strong> - 1 <br />
0 &le; <strong>D<sub>1</sub></strong>, <strong>A<sub>d</sub></strong>, <strong>B<sub>d</sub></strong> &le; 2 <br />
1 &le; <strong>S<sub>1</sub></strong>, <strong>A<sub>s</sub></strong>, <strong>B<sub>s</sub></strong> &le; 1,000,000,000 <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first case, your first learn a new spell that can defeat the first zombie with your first wand. This gives you 2 ways to defeat the first zombie with your first wand, and 1 way to defeat the second zombie with your second wand, for a total of 2 ways to defeat both.
</p>
<p>
You then learn three new ways to defeat the second zombie with your second wand. 2 ways to defeat the first zombie and 4 ways to defeat the second zombie is 8 ways to defeat both.
</p>
<p>
You then learn five new ways to defeat the second zombie with your first wand. You can't actually use your first wand to defeat the second zombie though, as that will leave you with no ways to defeat the first zombie. So you still have 8 ways to defeat both.
</p>
<p>
Finally, you learn seven new ways to defeat the first zombie with the second wand. If you use the first wand on the first zombie, and the second wand on the second zombie, you still have 8 combinations of possible spells. If you use the first wand on the second zombie and second wand on the first zombie, you have 7 * 5 = 35 combinations of possible spells. In total, that's 43 ways to defeat the zombies.
</p>
<p>
Therefore the total answer is 2 + 8 + 8 + 43 = 61.
</p>