File size: 3,603 Bytes
761e24d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<p>
Mr. Fox always puts aside some time on the weekends to practice his falconry. Mr. Fox owns <strong>N</strong> hawks, numbered from
1 to <strong>N</strong>. While numbering is somewhat impersonal, it quickly becomes infeasible to name each hawk individually when
you have as many hawks as Mr. Fox.
</p>
<p>
Every year, the local falconer club hosts a festival for falconers from across the nation. Mr. Fox shows off some of his hawks at each festival,
and this year is no different. Selecting a set of hawks to display is not a straightforward task however. Hawks can be temperamental creatures,
and they'll refuse to perform if they don't like the situation they find themselves in. Luckily, after careful study, Mr. Fox has been able to
capture the hawks' preferences in a simple boolean expression.
</p>
<p>
For example, let's say Mr. Fox has 4 hawks. Hawk 1 will only perform if some other hawk is present. Hawks 2 and 3 will only perform if hawks
1 or 4 are present. Hawk 4 is much more easy-going and will perform in all situations. We can express these preferences with the following
expression:
<pre>
((1 & (2 | 3)) | 4)
</pre>
</p>
<p>
Each number is a boolean variable indicating whether or not Mr. Fox brings that hawk. If the expression is satisfied, then all of the hawks he
brings will perform. If the expression is not satisfied, the hawks will be moody and that means no blue ribbons for Mr. Fox.
</p>
<p>
Mr. Fox is keen not to bore his audience, so he always brings a different set of hawks each year.
This is the <strong>K</strong>th annual festival, so he would like to bring the set of performing hawks with the <strong>K</strong>th
lowest value. Mr. Fox defines the value of a set of hawks as follows:
the empty set has a value of 0, and hawk <strong>i</strong> adds 2<sup><strong>i</strong></sup> to the value of a set.
So with 3 hawks, the sets in increasing order are:
<pre>
{1}
{2}
{1, 2}
{3}
{1, 3}
{2, 3}
{1, 2, 3}
</pre>
Note that Mr. Fox always brings a non-empty set of hawks.
</p>
<h3>Input</h3>
<p>
Input begins with an integer <strong>T</strong>, the number of festivals under consideration.
For each festival, there is first a line containing the space-separated integers <strong>N</strong> and <strong>K</strong>.
The next line contains the boolean expression encoding the hawks' preferences.
</p>
<h3>Output</h3>
<p>
For the <strong>i</strong>th festival, print a line containing "Case #<strong>i</strong>: " followed by
value of the set of hawks that Mr. Fox brings modulo 10<sup>9</sup>+7.
</p>
<h3>Constraints</h3>
<p>
1 ≤ <strong>T</strong> ≤ 20 <br />
1 ≤ <strong>N</strong> ≤ 200,000 <br />
1 ≤ <strong>K</strong> ≤ 10<sup>18</sup> <br />
Expressions contain no more than 2,500,000 characters each. <br />
It is guaranteed that there are at least <strong>K</strong> sets of performing hawks. <br />
</p>
<p>
The boolean expression adheres to the following grammar:
<pre>
[expression] ::= "(" "~" [expression] ")" | "(" [expression] [binary-operator] [expression] ")" | [variable]
[binary-operator] ::= "|" | "^" | "&"
[variable] ::= [digit] | [digit] [variable]
[digit] ::= "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
</pre>
Each hawk appears in the boolean expression exactly once. <br />
Whitespace may appear arbitrarily in the expression (except within variables) to improve readability. <br />
</p>
<h3>Explanation of Sample</h3>
<p>
In the first and second cases, the first 4 performing sets, in order, are {1, 2}, {1, 3}, {1, 2, 3}, and {4}, with values of 6, 10, 14, and 16 respectively.
</p>
|