---
id: 2067
lang: en
translation_of: index.md
source_sha256: f3861bc3d7aeee9b5807001811d47d7ad62cc0a9bd8d77429a753f9628e7fc6a
title: "Codeforces Round #522 (Div. 1, based on Technocup 2019 Elimination Round 3) - Problem-Solving Report"
description: "Short solutions with code for problems A to D of Codeforces Round #522 (Div. 1): Manhattan distance, knapsack counting, tree DP and doubling."
tags: ["OI","Codeforces"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "孤点所在的连块 rendered as “the component containing an isolated vertex”, reading 连块 as a slip for 连通块 (connected component)."
  - "然后在先用 ST 表 read as 然后再先用 ST 表 (“and then first use a sparse table”)."
  - "左右横跳 taken literally here (jumping left and right on the board), so no chip."
---

## A - Barcelonian Distance

First take the Manhattan distance as the answer, then enumerate the initial directions of the two points, and then just take the minimum.

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

using namespace std;

double a, b, c, x[3], y[3];

double calcByX(double x) { return (-1.0 * a * x - c) / b; }

double calcByY(double y) { return (-1.0 * b * y - c) / a; }

int main()
{
    scanf("%lf%lf%lf%lf%lf%lf%lf", &a, &b, &c, &x[1], &y[1], &x[2], &y[2]);
    double ans = fabs(x[1] - x[2]) + fabs(y[1] - y[2]);
    if (a != 0 && b != 0)
        for (int i = 0; i <= 1; i++)
            for (int j = 0; j <= 1; j++)
            {
                double pos1X, pos1Y, pos2X, pos2Y;
                double pans = 0;
                if (i == 0)
                    pos1X = calcByY(y[1]), pos1Y = y[1], pans += fabs(pos1X - x[1]);
                else
                    pos1X = x[1], pos1Y = calcByX(x[1]), pans += fabs(pos1Y - y[1]);

                if (j == 0)
                    pos2X = calcByY(y[2]), pos2Y = y[2], pans += fabs(pos2X - x[2]);
                else
                    pos2X = x[2], pos2Y = calcByX(x[2]), pans += fabs(pos2Y - y[2]);

                pans += sqrt((pos1X - pos2X) * (pos1X - pos2X) + (pos1Y - pos2Y) * (pos1Y - pos2Y));
                ans = min(ans, pans);
            }
    printf("%.10lf\n", ans);
    return 0;
}
```

<!--more-->

## B - The Unbearable Lightness of Weights

This problem asks us to compute the maximum number we can identify. We can observe:

- If there are only one or two kinds of mass, then the answer is just $n$.
- If there are more kinds of mass, then the final answer must be a knapsack of a single mass, because we need to determine exactly which kind it is. So, if such a single mass $w$ occurs $k$ times, and the final number of ways to choose is $cnt_w \choose k$, then $k$ can be taken as the answer.

Then do a knapsack and that’s it.

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

using namespace std;

const int MAX_N = 110, mod = 1e9 + 7;

int n, ai[MAX_N], cnt[MAX_N], binomial[MAX_N][MAX_N], ans = 1, upper, typ;
map<pair<int, int>, int> dp, pre;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), upper = max(upper, ai[i]), typ += ((++cnt[ai[i]]) == 1);
    for (int i = 0; i <= n; i++)
    {
        binomial[i][0] = binomial[i][i] = 1;
        for (int j = 1; j < i; j++)
            binomial[i][j] = (0LL + binomial[i - 1][j - 1] + binomial[i - 1][j]) % mod;
    }
    dp[make_pair(0, 0)] = 1, pre = dp;
    for (int i = 1; i <= n; i++)
    {
        for (auto &x : pre)
        {
            pair<int, int> curt = make_pair(x.first.first + 1, x.first.second + ai[i]);
            dp[curt] = (0LL + dp[curt] + x.second) % mod;
        }
        pre = dp;
    }
    if (typ == 1 || typ == 2)
        printf("%d\n", n);
    else
    {
        for (int w = 1; w <= upper; w++)
            for (int i = 2; i <= cnt[w]; i++)
            {
                int val = dp[make_pair(i, i * w)];
                if (dp[make_pair(i, i * w)] == binomial[cnt[w]][i])
                    ans = max(ans, i);
            }
        printf("%d\n", ans);
    }
    return 0;
}
```

## C - Vasya and Maximum Matching

Consider how to construct a graph with a unique perfect matching: we can make some vertices **isolated vertices**, where the component containing an isolated vertex is only itself, so that the remaining connected components all have perfect matchings. At the start we can delete them all, and then we consider valid ways of connecting edges: if a connected component has a unique perfect matching, then attaching a chain of two edges to this component also forms a valid connected component. Just do DP based on this property.

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

using namespace std;

const int MAX_N = 3e5 + 200, mod = 998244353;

int n, head[MAX_N], current, dp[MAX_N][3], tmp[3];

struct edge
{
    int to, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

void dfs(int u, int fa)
{
    dp[u][0] = 1;
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        if (edges[i].to != fa)
        {
            dfs(edges[i].to, u);
            tmp[0] = 1LL * dp[u][0] * ((0LL + dp[edges[i].to][0] + dp[edges[i].to][2]) % mod) % mod;
            tmp[1] = (1LL * dp[u][0] * dp[edges[i].to][2] % mod + 1LL * dp[u][1] * ((dp[edges[i].to][0] + 2LL * dp[edges[i].to][2] % mod) % mod) % mod) % mod;
            tmp[2] = (1LL * dp[u][0] * (dp[edges[i].to][0] + dp[edges[i].to][1]) % mod + 1LL * dp[u][1] * (dp[edges[i].to][0] + dp[edges[i].to][1]) % mod + 1LL * dp[u][2] * ((dp[edges[i].to][0] + 2LL * dp[edges[i].to][2] % mod) % mod) % mod) % mod;
            swap(tmp, dp[u]);
        }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1, u, v; i <= n - 1; i++)
        scanf("%d%d", &u, &v), addpath(u, v), addpath(v, u);
    dfs(1, 0);
    printf("%d\n", (dp[1][0] + dp[1][2]) % mod);
    return 0;
}
```

## D - Chattering

When I first thought about this problem I came up with a very brute-force transition, but in the end I found there were cycles, so it was <span class="slang" data-zh="GG" data-gloss="Gamer shorthand for “good game”, typed when conceding a match. Here: it is over, I am done for.">GG</span>.

We can consider using double binary lifting (?) to handle this. We can copy this board (?) once on each side, and then first use a sparse table to work out the information for jumping left and right. Once that is done, jump once more on top of it, and then when answering queries just process the binary bits from high to low.

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

using namespace std;

const int MAX_N = 3e5 + 200;

int n, ai[MAX_N], lft[20][MAX_N], rig[20][MAX_N], log2_[MAX_N];

struct ST
{
    int table[20][MAX_N], val[MAX_N], typ;

    int gmax(int x, int y) { return val[x] > val[y] ? x : y; }

    void build(int *arr, int len, int type)
    {
        typ = type;
        for (int i = 1; i <= len; i++)
            table[0][i] = i, val[i] = typ * arr[i];
        for (int i = 1; i <= 19; i++)
            for (int j = 1; j + (1 << i) - 1 <= len; j++)
                table[i][j] = gmax(table[i - 1][j], table[i - 1][j + (1 << (i - 1))]);
    }

    int query(int l, int r)
    {
        int d = log2_[r - l + 1];
        return gmax(table[d][l], table[d][r - (1 << d) + 1]);
    }
} lrmq, rrmq;

int main()
{
    scanf("%d", &n);
    for (int i = 2; i <= 3 * n; i++)
        log2_[i] = log2_[i >> 1] + 1;
    for (int i = 1; i <= n; i++)
        scanf("%d", &ai[i]), ai[i + n] = ai[i + (n << 1)] = ai[i];
    if (n == 1)
        return puts("0"), 0;
    for (int i = 1; i <= 3 * n; i++)
        lft[0][i] = max(1, i - ai[i]), rig[0][i] = min(3 * n, i + ai[i]);
    for (int i = 0; i <= log2_[3 * n]; i++)
        lft[i][1] = 1, rig[i][3 * n] = 3 * n;
    lrmq.build(lft[0], 3 * n, -1), rrmq.build(rig[0], 3 * n, 1);
    for (int i = 1; i <= log2_[3 * n]; i++)
        for (int j = 1; j <= 3 * n; j++)
        {
            int posl = lrmq.query(lft[i - 1][j], rig[i - 1][j]);
            int posr = rrmq.query(lft[i - 1][j], rig[i - 1][j]);
            lft[i][j] = min(lft[i - 1][posl], lft[i - 1][posr]);
            rig[i][j] = max(rig[i - 1][posl], rig[i - 1][posr]);
        }
    for (int j = n + 1; j <= (n << 1); j++)
    {
        int wl = j, wr = j, ans = 0;
        for (int i = log2_[3 * n]; i >= 0; i--)
        {
            if (max(rig[i][wl], rig[i][wr]) - min(lft[i][wl], lft[i][wr]) + 1 >= n)
                continue;
            int wwl = lrmq.query(lft[i][wl], rig[i][wr]);
            int wwr = rrmq.query(lft[i][wl], rig[i][wr]);
            ans += (1 << i), wl = wwl, wr = wwr;
        }
        ans++;
        printf("%d ", ans);
    }
    puts("");
    return 0;
}
```
