Olympiad in Informatics

Codeforces Round #493 (Div. 1) - Problem-Solving Report

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.

// 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

Printing a table shows that from term \(11\) on, the difference is always \(49\).

// 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.

// 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;
}