---
id: 2195
lang: en
translation_of: index.md
source_sha256: 126dfb38389896001d7f322303f7cc699c9524bd32a518a2ec08c3f99b2c98b0
title: "Codeforces Round #493 (Div. 1) - Problem-Solving Report"
description: "Solutions to Div. 1 A, B and C: compress and count pairs, read a pattern off a table of small cases, and collapse inclusion–exclusion with the binomial theorem."
tags: ["OI","Math","Codeforces","Inclusion–Exclusion","Table Hardcoding"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "B: 打表，发现 is chipped as a whole phrase in the make-a-table-and-spot-the-pattern sense (as 打表发现 is in other posts), rather than with the glossary's 打表 hardcoding gloss; the code does also hardcode the first terms."
  - "C: 「至少转恰好」 rendered as “at least to exactly”, the usual at-least-to-exactly conversion in inclusion–exclusion."
---

## A - Convert to Ones

After compressing, count how many $01$ pairs there are, then just multiply by the smaller cost and add the cost of one flip.

<!--more-->

```cpp
// A.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 3e5 + 200;

int n, x, y, cnt;
char str[MAX_N];

int main()
{
    scanf("%d%d%d%s", &n, &x, &y, str + 1);
    str[0] = '1';
    for (int i = 1; i <= n; i++)
        cnt += (str[i] == '0' && str[i - 1] == '1');
    long long ans = 0;
    if (cnt == 0)
        ans = 0;
    else
        ans = 1LL * (cnt - 1) * min(x, y) + y;
    printf("%lld\n", ans);
    return 0;
}
```

## B - Roman Digits

<span class="slang" data-zh="打表，发现" data-gloss="dǎbiǎo fāxiàn, “make a table and discover”. Here: brute-forcing answers for small inputs and spotting the pattern in them.">Printing a table shows</span> that from term $11$ on, the difference is always $49$.

```cpp
// B.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e5 + 200, series[12] = {0, 4, 10, 20, 35, 56, 83, 116, 155, 198, 244, 292};

int main()
{
    int n;
    scanf("%d", &n);
    if (n <= 11)
        printf("%d\n", series[n]);
    else
        printf("%lld\n", series[11] + 1LL * (n - 11) * 49);
    return 0;
}
```

## C - Sky Full of Stars

Now this problem is rather more interesting.

Consider computing it with inclusion–exclusion. Normally, when we count “at least $i$ rows and $j$ columns”, we get:

$$
{n \choose i} {n \choose j} f(i, j)
$$

where $f(i, j)$ is the number of ways to fill the rest arbitrarily. However, this way of counting overlaps **with itself**, so when we do inclusion–exclusion we are not removing the “at least to exactly” kind of overlap; instead, we apply inclusion–exclusion to the part that repeats within itself and that also shows up the same number of times for different $i, j$. The coefficient is simply a $-1$.

So now we can do inclusion–exclusion as usual:

$$
ans = \sum_{i = 0}^n \sum_{j = 0}^n (-1)^{i + j + 1} {n \choose i} {n \choose j} [i + j > 0] f(i, j)
$$

Consider the value of $f(i, j)$. If $i > 0, j > 0$, it is easy to see that it is $3^{(n - i)(n - j)} \times 3$. If one of the dimensions is $0$, then the number of choices actually becomes $3^i 3^{n^2 - ni}$. Substituting in, we get:

$$
ans = \sum_{i = 1}^n \sum_{j = 1}^n (-1)^{i + j + 1} {n \choose i} {n \choose j} 3^{(n - i)(n - j)} + 2\sum_{i = 1}^n (-1)^{i + 1} {n \choose i} 3^i 3^{n^2 - ni}
$$

The latter part is easy to compute in $\Theta(n \log n)$ time; the key is the first term, which looks like $\Theta(n^2)$ no matter how you look at it. We pull some things out and group them into the outermost sums:

$$
\begin{aligned} part &= \sum_{i = 1}^n \sum_{j = 1}^n (-1)^{i + j + 1} {n \choose i} {n \choose j} 3^{(n - i)(n - j)} \\ &= (-1) \sum_{i = 1}^n (-1)^i {n \choose i} \sum_{j = 1}^n (-1)^j {n \choose j} 3^{n^2 - in - jn + ij} \\ &= (-1) 3^{n^2} \sum_{i = 1}^n (-1)^i {n \choose i} 3^{-in} \sum_{j = 1}^n (-1)^j {n \choose j} 3^{(i - n)j} \end{aligned}
$$

The trailing $\sum_{j = 1}^n$ part can be merged directly with the binomial theorem:

$$
\begin{aligned} part &= (-1) 3^{n^2} \sum_{i = 1}^n (-1)^i {n \choose i} 3^{-in} \sum_{j = 1}^n (-1)^j {n \choose j} 3^{(i - n)j} \\ &= (-1) 3^{n^2} \sum_{i = 1}^n (-1)^i {n \choose i} 3^{-in} [ (1 - 3^{i - n})^n - 1] \end{aligned}
$$

And that’s it.

```cpp
// C.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 1e6 + 200, mod = 998244353;

int n, fac[MAX_N], fac_inv[MAX_N], inv[MAX_N];

int fpow(int bas, int tim)
{
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % mod;
        bas = 1LL * bas * bas % mod;
        tim >>= 1;
    }
    return ret;
}

int binomial(int n_, int k_) { return 1LL * fac[n_] * fac_inv[k_] % mod * fac_inv[n_ - k_] % mod; }

int main()
{
    scanf("%d", &n);
    inv[0] = inv[1] = 1;
    for (int i = 2; i <= n; i++)
        inv[i] = 1LL * inv[mod % i] * (mod - mod / i) % mod;
    for (int i = fac[0] = fac_inv[0] = 1; i <= n; i++)
        fac[i] = 1LL * fac[i - 1] * i % mod, fac_inv[i] = 1LL * fac_inv[i - 1] * inv[i] % mod;
    int ans = 0;
    for (int i = 1, opt = 1; i <= n; i++, opt = mod - opt)
        ans = (0LL + ans + 1LL * opt * binomial(n, i) % mod * fpow(3, i) % mod * fpow(3, 1LL * n * (n - i) % (mod - 1)) % mod) % mod;
    ans = 2LL * ans % mod;
    int part = 0;
    for (int i = 1, opt = mod - 1; i <= n; i++, opt = mod - opt)
    {
        int tmpi = 1LL * opt * binomial(n, i) % mod * fpow(3, ((mod - 1) - 1LL * n * i % (mod - 1)) % (mod - 1)) % mod;
        int tmpj = (0LL + fpow((1 + mod - fpow(3, (i + mod - 1 - n) % (mod - 1))) % mod, n) + mod - 1) % mod;
        part = (0LL + part + 1LL * tmpi * tmpj % mod) % mod;
    }
    part = 1LL * part * 3LL % mod * fpow(3, 1LL * n * n % (mod - 1)) % mod * (mod - 1) % mod;
    printf("%lld\n", (0LL + part + ans) % mod);
    return 0;
}
```
