---
id: 2570
lang: en
translation_of: index.md
source_sha256: 18cdfa9d22021d67272cf2f481a9a2a036414b94ef6963dfacd96ea1d571a252
title: "AtCoder Grand Contest 043 - Problem-Solving Report"
description: "A brain-dead problem: the final path must be a concatenation of black and white sub-paths, so its cost is the number of times it enters a black one."
tags: ["OI","Graph Theory","Game Theory","Ad Hoc","SG"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "真你妈的屌 rendered as “truly fucking badass”; only 屌 is chipped, 你妈的 is translated as the plain profanity “fucking”."
---

## A - Range Flip Find Route

<span class="slang" data-zh="思博" data-gloss="sībó, a homophone of SB, i.e. 傻逼 (shǎbī), “idiot”. Here: dead simple, dumb.">Brain-dead</span> problem. Consider that the final path must be a concatenation of several black and white sub-paths, so the cost of the path is the number of times it enters a black sub-path, so all we need is to give the edges going from white into black a weight of $1$ when building the graph.

<!--more-->

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

using namespace std;

const int MAX_N = 110, dx[2] = {0, 1}, dy[2] = {1, 0};

typedef pair<int, int> pii;

int n, m, idx[MAX_N][MAX_N], head[MAX_N * MAX_N], current, dist[MAX_N * MAX_N], start_pos;
char str[MAX_N][MAX_N];
bool vis[MAX_N * MAX_N];

struct edge
{
    int to, nxt, weight;
} edges[MAX_N * MAX_N << 3];

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

void shortestPath()
{
    memset(dist, 0x3f, sizeof(dist));
    priority_queue<pii> pq;
    pq.push(make_pair(0, start_pos)), dist[start_pos] = 0;
    while (!pq.empty())
    {
        int u = pq.top().second;
        pq.pop();
        if (vis[u])
            continue;
        vis[u] = true;
        for (int i = head[u]; i != -1; i = edges[i].nxt)
            if (dist[edges[i].to] > dist[u] + edges[i].weight)
                dist[edges[i].to] = dist[u] + edges[i].weight, pq.push(make_pair(-dist[edges[i].to], edges[i].to));
    }
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
        scanf("%s", str[i] + 1);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            idx[i][j] = m * (i - 1) + j;
    start_pos = n * m + 1;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            for (int d = 0; d < 2; d++)
            {
                int dstx = i + dx[d], dsty = j + dy[d];
                if (str[dstx][dsty] != 0)
                    if (str[i][j] == '.')
                        if (str[dstx][dsty] != str[i][j])
                            addpath(idx[i][j], idx[dstx][dsty], 1);
                        else
                            addpath(idx[i][j], idx[dstx][dsty], 0);
                    else
                        addpath(idx[i][j], idx[dstx][dsty], 0);
            }
    addpath(start_pos, idx[1][1], str[1][1] == '#');
    shortestPath(), printf("%d\n", dist[idx[n][m]]);
    return 0;
}
```

## B - 123 Triangle

A counting-contributions + pattern-finding problem.

First, we can convert the input $1, 2, 3$ into $0, 1, 2$ (the differences don't change). Then let's first consider determining the parity of the answer; at its core, this subproblem is really XOR-ing a bunch of $0, 1$. This triangle is really just Pascal's triangle, so the contribution is ${n - 1 \choose i}$. So we can consider using Lucas to compute the parity of this binomial coefficient, and then put it into the answer.

If it finally turns out to be odd, then obviously the answer is $1$; if it's not odd, there's a property: if $1$ appears in the original sequence, the answer is $0$, otherwise it's $2$. If the original sequence has only $0, 2$, then this problem can totally be viewed as the $0, 1$ subproblem, and then we multiply the answer back by a $2$.

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

using namespace std;

const int MAX_N = 1e6 + 200;

int n, ai[MAX_N], fac[MAX_N];
char str[MAX_N];

int binomial(int n_, int k_) { return fac[n_] - fac[k_] - fac[n_ - k_]; }

int main()
{
    scanf("%d%s", &n, str + 1);
    bool flag = false;
    for (int i = 1; i <= n; i++)
        ai[i] = str[i] - '1', flag |= (ai[i] == 1);
    for (int i = 1; i <= n; i++)
    {
        int acc = i;
        while (!(acc & 1))
            fac[i]++, acc >>= 1;
        fac[i] += fac[i - 1];
    }
    if (!flag)
        for (int i = 1; i <= n; i++)
            ai[i] >>= 1;
    int ans = 0;
    for (int i = 1; i <= n; i++)
        if ((ai[i] & 1) && (!binomial(n - 1, i - 1)))
            ans ^= 1;
    if (!flag)
        ans <<= 1;
    printf("%d\n", ans);
    return 0;
}
```

## C - Giant Graph

This problem is truly fucking <span class="slang" data-zh="屌" data-gloss="diǎo, a vulgar word for penis. Crude slang for awesome or impressive.">badass</span>.

First, we definitely know one thing: because this exponent is set <span class="slang" data-zh="贼" data-gloss="zéi, “thief”. A northern colloquial intensifier meaning “really” or “super”.">damn</span> large, we should use the bitwise greedy idea and determine $i + j + k$ from largest to smallest, and then this graph can be connected into a DAG. We now need to find an independent set.

Then this problem starts getting <span class="slang" data-zh="骚" data-gloss="sāo, “flirty, showy”. Slang praise for something flashy, clever and unconventional.">slick</span>: this is a game graph, and the state points of the optimal solution are themselves the maximum independent set. We can consider computing these SG function values, and then computing the value of $SG(x) \oplus SG(y) \oplus SG(z) = 0$ in the graph. We find that $SG(x) \leq \sqrt{m}$, so we can consider just computing it by brute force.

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

using namespace std;

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

typedef long long ll;

int n, m[3], base, vis[MAX_N], sg[MAX_N];
vector<int> G[MAX_N], sum[3];

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 main()
{
    scanf("%d", &n), base = fpow(ll(1e18) % mod, mod - 2);
    for (int rd = 0; rd < 3; rd++)
    {
        scanf("%d", &m[rd]);
        for (int i = 1; i <= n; i++)
            G[i].clear();
        for (int i = 1, u, v; i <= m[rd]; i++)
        {
            scanf("%d%d", &u, &v);
            if (u > v)
                swap(u, v);
            G[u].push_back(v);
        }
        memset(sg, 0, sizeof(sg)), memset(vis, -1, sizeof(vis));
        int cbase = fpow(10, 18 * n);
        for (int i = n; i >= 1; i--)
        {
            for (int v : G[i])
                vis[sg[v]] = i;
            while (vis[sg[i]] == i)
                sg[i]++;
            while (sum[rd].size() <= sg[i])
                sum[rd].push_back(0);
            sum[rd][sg[i]] = (0LL + sum[rd][sg[i]] + cbase) % mod, cbase = 1LL * cbase * base % mod;
        }
    }
    int ans = 0;
    for (int i = 0, siz1 = sum[0].size(); i < siz1; i++)
        for (int j = 0, siz2 = sum[1].size(); j < siz2; j++)
        {
            int k = i ^ j;
            if (k < sum[2].size())
                ans = (0LL + ans + 1LL * sum[0][i] * sum[1][j] % mod * sum[2][k] % mod) % mod;
        }
    printf("%d\n", ans);
    return 0;
}
```

## D - Merge Triplets

Consider the properties of the generated sequence. For an original sequence $A_i = (X_1, X_2, X_3)$, if $X_i > X_{i + 1}$ holds, then these two elements are dead certain to be consecutive in the sequence $P$; of course, if $X_1 > X_2, X_1 > X_3$ holds, they are also dead certain to be consecutive. Then such an $X_i$ can serve as the start of a block, subdividing the triple. Then these small blocks are really just substrings of the target sequence, one after another.

So a target permutation $P$ can be counted if and only if there exists a way of splitting it into blocks such that the blocks join up into several units, and each unit has $3$ elements.

Once the block split is determined, are the elements fixed too? Obviously, yes. First, the order within each block is already determined, and each time a block is placed, the first number of this block must be greater than the first numbers of all previous blocks. And since the first number is the largest number of the block, at least the order between blocks is already confirmed; and, the numbers within a block are also decreasing, so the order within blocks is determined too. This is definitely a unique permutation.

So we only need to count the block splits. What kind of block split is valid? Either split into blocks of $3, 2, 1$, but note that the number of blocks of length $2$ must be less than the number of blocks of length $1$, so that there is a perfect matching. Let $dp[i][j]$, where $i$ is the last position and $j$ is the number of blocks of length $1$ minus the number of blocks of length $2$; the setup is a bit like CSP-S 2019 D2T1. Finally, just take the ones with $\geq 0$.

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

using namespace std;

const int MAX_N = 2020, base = MAX_N;

int n, mod, dp[MAX_N * 3][MAX_N * 4];

int main()
{
    scanf("%d%d", &n, &mod), dp[0][base] = 1;
    for (int i = 0; i < 3 * n; i++)
        for (int j = -n; j <= 3 * n; j++)
        {
            dp[i + 1][j + 1 + base] = (0LL + dp[i + 1][j + 1 + base] + dp[i][j + base]) % mod;
            dp[i + 2][j - 1 + base] = (0LL + dp[i + 2][j - 1 + base] + 1LL * dp[i][j + base] * (i + 1) % mod) % mod;
            dp[i + 3][j + base] = (0LL + dp[i + 3][j + base] + 1LL * dp[i][j + base] * (i + 2) % mod * (i + 1) % mod) % mod;
        }
    int ans = 0;
    for (int i = 0; i <= 3 * n; i++)
        ans = (0LL + ans + dp[3 * n][i + base]) % mod;
    printf("%d\n", ans);
    return 0;
}
```
