J. ABC

Codeforces
IDCF10288J
Time1000ms
Memory256MB
Difficulty
English · Original
Formal · Original
You're given an string $s$ of length $n$. Each character in it is either '$a$', '$b$', or '$c$'. In addition, the string contains at most $1$ '$b$' character. You can swap the values of any two indices. Find the minimum number of swaps needed so that for every index $i$ $(1 <= i <= n -1)$, either $s_i = s_{i + 1}$ or $s_i$ = '$b$' or $s_{i + 1}$ = '$b$'. The first line contains a single integer $n$ $(1 <= n <= 10^5)$ — the length of the string. The second line contains the string $s$. If there's no way to satisfy the condition in the statement, print "-1". Othwerwise, print the minimum number of swaps to achieve the condition. ## Input The first line contains a single integer $n$ $(1 <= n <= 10^5)$ — the length of the string.The second line contains the string $s$. ## Output If there's no way to satisfy the condition in the statement, print "-1". Othwerwise, print the minimum number of swaps to achieve the condition. [samples]
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the number of strings. Let $ S = \{s_1, s_2, \dots, s_n\} $ be a set of non-empty strings over lowercase English letters. Let $ \mathcal{L} = \bigcup_{i=1}^n \{ \text{non-empty proper substrings of } s_i \} $ be the multiset of all non-empty proper substrings across all strings. For each string $ s_i $, define: - $ c_i = |\{ s_j \in S \mid s_j \text{ is a non-empty proper substring of } s_i \}| $, the number of strings in $ S $ that appear as non-empty proper substrings of $ s_i $. - $ t_i = \sum_{u \in \text{substrings}(s_i),\, u \ne s_i} \mathbf{1}_{u \in S} $, the total count of occurrences of strings in $ S $ as non-empty proper substrings of $ s_i $. **Constraints** 1. $ 1 \le n \le 10^5 $ 2. $ \sum_{i=1}^n |s_i| \le 10^6 $ 3. All characters in $ s_i $ are lowercase English letters. 4. Transition from $ s_i $ to $ s_j $ is possible iff $ s_j $ is a non-empty proper substring of $ s_i $, with probability proportional to the number of times $ s_j $ appears as a substring in $ s_i $. 5. The process stops when no non-empty proper substring of the current string belongs to $ S $. **Objective** Let $ E_i $ denote the expected number of strings visited starting from $ s_i $. Then: $$ E_i = 1 + \sum_{\substack{s_j \in S \\ s_j \text{ is a proper substring of } s_i}} \left( \frac{\text{count of } s_j \text{ in } s_i}{t_i} \cdot E_j \right), \quad \text{if } t_i > 0 $$ $$ E_i = 1, \quad \text{if } t_i = 0 $$ The answer is the average over initial choices: $$ \mathbb{E} = \frac{1}{n} \sum_{i=1}^n E_i $$ Compute $ \mathbb{E} \mod 998244353 $, expressed as $ p \cdot q^{-1} \mod 998244353 $, where $ \mathbb{E} = \frac{p}{q} $ in lowest terms.
API Response (JSON)
{
  "problem": {
    "name": "J. ABC",
    "description": {
      "content": "You're given an string $s$ of length $n$. Each character in it is either '$a$', '$b$', or '$c$'. In addition, the string contains at most $1$ '$b$' character.  You can swap the values of any two indi",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF10288J"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You're given an string $s$ of length $n$. Each character in it is either '$a$', '$b$', or '$c$'. In addition, the string contains at most $1$ '$b$' character. \n\nYou can swap the values of any two indi...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the number of strings.  \nLet $ S = \\{s_1, s_2, \\dots, s_n\\} $ be a set of non-empty strings over lowercase English letters.  \nLet $ \\mathcal{L} = \\bigcu...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments