ImportantSequence
SRM 540 · 2011-11-22 · by nV
SRM 540 · 2011-11-22 · by nV · Simple Search, Iteration
Problem Statement
Problem Statement
This problem statement contains superscripts and/or subscripts. It may not display properly outside the applet.
Little Rudolph had an important sequence of positive integers. The sequence consisted of N positive integers a0, a1, .., aN-1.
Rudolph wrote the sequence onto the blackboard in the classroom. While Rudolph had gone out, little Arthur came into the classroom and saw the sequence. Arthur likes to play with numbers as much as he likes to give his friends puzzles. So he did the following:
- First, he wrote a '+' or a '-' between each pair of consecutive numbers (possibly using different signs for different pairs of numbers).
- Next, for each sign he computed the result of the corresponding operation and wrote it under the sign. I.e., if he used the '+' sign between ai and ai+1, he would write the sum ai+ai+1 under this '+' sign. Similarly, if he used the '-' sign between ai and ai+1, he would write the difference ai-ai+1. In this way he obtained a new sequence of N-1 numbers b0, b1, .., bN-2.
- Finally, he erased the original sequence. Now there was only the operator sequence o0, o1, .., oN-2 and the resulting number sequence b0, b2, .., bN-2 left on the blackboard.
1 2 3 4 -> 1 + 2 - 3 + 4 -> 1 + 2 - 3 + 4 -> + - +
3 -1 7 3 -1 7
When Rudolph returned, he was shocked as his important sequence had disappeared. Arthur quickly told him what operations he had performed and that Rudolph has to simply reconstruct the orginal sequence.
Unfortunately, little Arthur did not realize that it is not necessarily possible to determine the original sequence uniquely. For example, both original sequences {1, 2, 3, 4} and {2, 1, 2, 5} lead to the same sequence {3, -1, 7} when operator sequence is {+, -, +}.
The only thing Rudolph remembers about his original sequence is that all the integers were positive. Rudolph now wants to count all sequences of positive integers that match the blackboard. You are given
Notes
- It is guaranteed that the correct answer will always fit into the 32-bit signed integer type.
- The integer 0 (zero) is not positive. It may not occur in Rudolph's original sequence.
Constraints
- B will contain between 1 and 50 elements, inclusive.
- operators will contain the same number of characters as the number of elements in B.
- Each element of B will be between -1000000000 (-109) and 1000000000 (109), inclusive.
- Each character in operators will be either '+' or '-' (quotes for clarity).
Examples
0)
{3, -1, 7}
"+-+"
Returns: 2
From the problem statement.
1)
{1}
"-"
Returns: -1
There are infinitely many pairs of positive integers that differ by one.
2)
{1}
"+"
Returns: 0
Note that all numbers ai have to be positive integers.
3)
{10}
"+"
Returns: 9
4)
{540, 2012, 540, 2012, 540, 2012, 540}
"-+-+-+-"
Returns: 1471
Submissions are judged against all 330 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class ImportantSequence with a public method int getCount(vector<int> B, string operators) · 330 test cases · 2 s / 256 MB per case