Place postcards in random batches, flipping a batch when its top card is upside down, and compute the expected number left picture down.
Medium7ProbabilityDynamic programmingCombinatoricsNo attempts yetTime limit2sMemory limit512 MBFedor travels often, and over the years he has collected postcards from all over the world. Each postcard has a picture on one side and space for an address and a message on the other side.
During a party at his house, Fedor decided to lay every postcard on the table so the guests can see them. The postcards start as a single stack in his hands, and some of them sit upside down, picture facing down. Instead of inspecting each postcard and turning it over one at a time, Fedor repeats the following procedure.
Turning k postcards over at once reverses the orientation of every postcard in the group. A postcard that was picture up ends picture down, and a postcard that was picture down ends picture up. Each choice of k is independent of the earlier choices.
Compute the expected number of postcards that lie picture down on the table when the procedure ends.
The first line contains a string s made only of the characters C and W. The i-th character of s describes the i-th postcard counted from the top of the starting stack. C is a postcard that is picture up, and W is a postcard that is picture down. The length of s is between 1 and 200000.
Print on one line the expected number of postcards that lie picture down on the table, rounded to exactly six digits after the decimal point. Print all six digits, including trailing zeros.