|
<p> |
|
It's time for your school class to elect its class officers for the year. These officers will have the honour of representing the class and will hold various responsibilities. |
|
The position of class treasurer is particularly appealing to some of the more organized and mathematically-inclined students, especially two rivals, Amy and Betty. |
|
They've both been running strong campaigns, to the point that no other feasible candidates remain. |
|
</p> |
|
|
|
<p> |
|
A democratic vote to elect the class treasurer is about to take place! Each of the <strong>N</strong> students in the class will cast one vote, for either Amy or Betty. |
|
The students have IDs numbered from 1 to <strong>N</strong>, and student <em>i</em> is currently planning on voting for either |
|
Amy (if <strong>V<sub>i</sub></strong> = "A") or Betty (if <strong>V<sub>i</sub></strong> = "B"). |
|
</p> |
|
|
|
<p> |
|
Your teacher, Mr. X, has some "novel" ideas about how elections should work. Perhaps in an effort to teach your class a statistics lesson, he will organize the election as follows. |
|
He'll consider the set of all <strong>N</strong>*(<strong>N</strong>+1)/2 possible non-empty contiguous sets of student IDs, and will select one at random. |
|
Each set will have an equal chance of being selected. He'll refer to this as the "representative set" of students. He will also announce a threshold of victory, <strong>K</strong>. |
|
He'll then tally up the number of votes for Amy and Betty amongst those students — let these vote counts be <strong>a</strong> and <strong>b</strong> respectively. |
|
If <strong>a</strong> > <strong>b</strong> + <strong>K</strong>, then Amy will win. |
|
If <strong>b</strong> > <strong>a</strong> + <strong>K</strong>, then Betty will win. |
|
Otherwise, if |<strong>a</strong> - <strong>b</strong>| ≤ <strong>K</strong>, then it will be a draw (neither candidate will win, and the class will be left without a treasurer). |
|
</p> |
|
|
|
<p> |
|
Amy has grown concerned about how the election will turn out, so she's enlisted your help in potentially swaying some of your classmates' opinions. |
|
She's tasked you with ensuring that, no matter which representative set gets chosen, Betty cannot possibly win (in other words, either Amy will win or neither candidate will win). |
|
To do so, you may pay 0 or more students to change their vote from their current candidate to the other one. |
|
Student <em>i</em> requires 2<sup>i</sup> dollars to be influenced in this fashion. You must finish paying students off <i>before</i> you know what the representative set will be. |
|
</p> |
|
|
|
<p> |
|
What's the minimum possible cost required to guarantee that Betty cannot possibly win and become the class treasurer? |
|
As this cost may be large, output it modulo 1,000,000,007. |
|
Note that you must minimize the actual cost, rather than minimizing the resulting value of the cost after it's taken modulo 1,000,000,007. |
|
</p> |
|
|
|
|
|
<h3>Input</h3> |
|
|
|
<p> |
|
Input begins with an integer <strong>T</strong>, the number of elections. |
|
For each election, there are two lines. The first line contains the space-separated integers <strong>N</strong> and <strong>K</strong>. |
|
The second line contains the <strong>N</strong> characters <strong>V<sub>1</sub></strong> through <strong>V<sub>N</sub></strong>. |
|
</p> |
|
|
|
|
|
<h3>Output</h3> |
|
|
|
<p> |
|
For the <em>i</em>th election, print a line containing "Case #<em>i</em>: " |
|
followed by 1 integer, the minimum possible cost (in dollars) required to guarantee that Betty cannot become the class treasurer, modulo 1,000,000,007. |
|
</p> |
|
|
|
|
|
<h3>Constraints</h3> |
|
|
|
<p> |
|
1 ≤ <strong>T</strong> ≤ 200 <br /> |
|
1 ≤ <strong>N</strong> ≤ 1,000,000 <br /> |
|
0 ≤ <strong>K</strong> ≤ <strong>N</strong> <br /> |
|
</p> |
|
|
|
<h3>Explanation of Sample</h3> |
|
|
|
<p> |
|
In the first case, if you do nothing, then Betty may win (if the representative set either consists of only student 1 or only student 4). |
|
You should pay students 1 and 4 to each vote for Amy instead of Betty, for a cost of $2 + $16 = $18. Amy will then be guaranteed to win for any choice of representative set. |
|
</p> |
|
|
|
<p> |
|
In the second case, you don't need to pay any students to change their minds — either Amy will win, or it will be a draw. |
|
</p> |
|
|
|
<p> |
|
In the third case, you should pay student 2 $4 to change their vote to Amy. |
|
</p> |
|
|