#P2614. Calculator Music Duration
Calculator Music Duration
Calculator Music Duration
You are given a musical score written in a special calculator notation and an integer T
representing the beats per minute (BPM). Your task is to compute the total play time in seconds required to execute the score.
The score is represented as a string and its note durations are determined as follows:
- A standalone note (any key) without extra symbols (for example,
1
) represents a quarter note, which lasts for1
beat. - If the note is followed by one or more dashes (
-
), each dash increases the duration by 1 beat. For example,1-
lasts for2
beats and1---
lasts for4
beats (a whole note). Note that a whole note (4 beats) will never have a dot. - To represent notes with durations shorter than a quarter note, nested parentheses are used. A note enclosed in one pair of parentheses is an eighth note (i.e. its base duration is halved), in two pairs is a sixteenth note (divided by 4), and in three pairs is a thirty-second note (divided by 8). In general, if a note is nested in L levels of parentheses, its computed duration is multiplied by \(\frac{1}{2^L}\).
For example, in the musical phrase(1(34(56))2)
:- The notes
1
and2
(inside one pair) last \(\frac{1}{2}\) beat each. - The notes
3
and4
(inside two pairs) last \(\frac{1}{4}\) beat each. - The notes
5
and6
(inside three pairs) last \(\frac{1}{8}\) beat each.
- The notes
- A note may be followed by a dot (
.
), which increases its duration by half of its current value. For instance,1.
lasts for \(1+\frac{1}{2}=1.5\) beats and1-.
lasts for \(2+1=3\) beats.
Arbitrary spaces and newlines in the score should be ignored.
The total play time is computed by summing the beat durations of all notes, and then using the formula:
[ \text{Total seconds} = \frac{\text{Total beats} \times 60}{T} ]
Print the total time in seconds.
inputFormat
Input consists of two lines. The first line is a string representing the musical score (which may contain spaces and newlines that should be ignored). The second line contains an integer T
(beats per minute).
outputFormat
Output the total play time in seconds as a number. The output may be fractional.
sample
1
60
1