#P3334. [ZJOI2013] 抛硬币
[ZJOI2013] 抛硬币
Description
There is a coin, with the probability of getting heads H being , and the probability of getting tails T being . 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 , what is the expected number of tosses needed for the sequence to contain the target sequence HT. However, after thinking for 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 . Let be the expected number of tosses to obtain HT, then we have:
Solving gives , so the expected number of tosses to obtain HT is .
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 , as defined in the description.
The next line contains a string consisting only of H and T, representing the target sequence to obtain.
Output Format
Output a single line , where and are positive integers and are coprime, representing the expected number of tosses needed to obtain the target sequence .
Note that if is , do not omit /1.
1 2
HT
4/1
Hint
For of the testdata, .
For of the testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号