#P12629. [NAC 2025] Popping Balloons

    ID: 14178 远端评测题 15000ms 2048MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>2025多项式分治快速傅里叶变换 FFTICPCNAC

[NAC 2025] Popping Balloons

题目描述

The ICPC logo has three colors: blue, yellow, and red. The NAC volunteers have just inflated a huge number of balloons in these colors and arranged them in a line. They next need to sort the balloons by color before they can give them out to contestants.

Unfortunately, due to the Orlando heat, the balloons begin to randomly pop: each second, a random remaining balloon pops (and the volunteers remove the debris from the line). This isn't all bad: maybe if the NAC volunteers wait long enough, the balloons will become sorted by chance? Compute the expected number of seconds until the first time that all blue balloons come before all yellow and red balloons, and all yellow balloons come before all red balloons. (These conditions are satisfied even if they are vacuously true: for example, if there are no blue balloons at all remaining, then it is true that all blue balloons come before all yellow and red balloons.)

输入格式

The input has one line: a string ss (1s21051\le |s|\le 2\cdot 10^5) where each character is one of B\texttt{B}, Y\texttt{Y}, or R\texttt{R} representing blue, yellow, and red respectively ---the colors of the initial balloons in the line.

输出格式

Print the expected number of seconds that elapse before the first time that all blue balloons come before all yellow and red balloons, and all yellow balloons come before all red balloons. Since this number might not be an integer, print it modulo 998244353998\, 244\, 353.

Formally, let p=998244353p = 998\,244\,353. It can be shown that the answer can be expressed as an irreducible fraction ab\frac{a}{b}, where aa and bb are non-negative integers and b≢0(modp)b \not \equiv 0 \pmod{p}. Print the integer xx with 0x<p0 \leq x < p and xab1modpx \equiv a \cdot b^{-1} \bmod p.

RYBB
831870297
YRBBR
598946615

提示

In Sample Input 1, the expected time until the balloon colors first become sorted in the correct order is $\frac {17}{6} = 2\cdot \frac{1}{6} + 3 \cdot \frac{5}{6}$ seconds: the only way for the balloon colors to be sorted correctly after 22 seconds is if the first two balloons to pop are the yellow and red balloon (in either order). The probability that these balloons pop before either blue balloon is 16\frac{1}{6}. Otherwise (with probability 56\frac{5}{6}) the balloon colors will automatically be sorted after 33 seconds, when there is only one balloon left. Since 61166374059(modp)6^{-1} \equiv 166\,374\,059\pmod {p}, the answer is 17166374059831870297(modp)17\cdot 166\,374\,059\equiv 831\,870\,297\pmod{p}.