#P3334. [ZJOI2013] 抛硬币

[ZJOI2013] 抛硬币

Description

There is a coin, with the probability of getting heads H being ab\frac{a}{b}, and the probability of getting tails T being 1ab1-\frac{a}{b}. Now TT starts playing a coin-tossing game and records each result: heads as H and tails as T. Thus she obtains a coin-toss sequence like HTHHT…. She suddenly wonders: when both probabilities of heads and tails are 12\frac{1}{2}, what is the expected number of tosses needed for the sequence to contain the target sequence HT. However, after thinking for 11 second, she realizes that if the first toss is T, then she still expects to need the number of tosses to obtain HT; if the first toss is H, then she only needs the expected number of tosses to obtain T, which is clearly 22. Let xx be the expected number of tosses to obtain HT, then we have:

$$x=1+\left(\frac{1}{2}\times x+\frac{1}{2}\times 2\right)$$

Solving gives x=4x=4, so the expected number of tosses to obtain HT is 44.

After solving this much simpler problem, she begins to think about the general case, where the probabilities of heads and tails are not necessarily equal, and the target sequence is not necessarily HT. However, after a long time of hard thinking, she still gets nowhere, so she turns to you for help.

Input Format

The first line contains two numbers a,ba, b, as defined in the description.

The next line contains a string SS consisting only of H and T, representing the target sequence to obtain.

Output Format

Output a single line pq\frac{p}{q}, where pp and qq are positive integers and are coprime, representing the expected number of tosses needed to obtain the target sequence SS.

Note that if qq is 11, do not omit /1.

1 2
HT
4/1

Hint

For 50%50\% of the testdata, a=1,b=2,S20a=1, b=2, |S|\le 20.

For 100%100\% of the testdata, a<b,b100,S1000a<b, b\le 100, |S|\le 1000.

Translated by ChatGPT 5