---
id: 2542
lang: en
translation_of: index.md
source_sha256: 3dfef5d468578c5010c4bfc3dedc325f87ce853d8838986c632acc50116003f3
title: "AtCoder Grand Contest 045 - Problem-Solving Report"
description: "AGC 045 A and B: a linear basis built from the back decides Xor Battle, and 01 Unbalanced enumerates the prefix-sum minimum near its global minimum."
tags: ["OI","Linear Basis","AtCoder"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## A - Xor Battle

Feels kind of <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span>.

For 0, we just need the final answer to be 0. So we maintain things from back to front: maintain the linear basis formed by combining the values at the $0$ positions, and each time we reach a $1$ position, just query it in there. If it isn't found, then for $1$, the later $0$s aren't enough to cancel that operation, so we can declare $1$ the winner, and vice versa.

<!--more-->

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

using namespace std;

const int MAX_N = 220;

typedef long long ll;

int T, n;
ll base[MAX_N], ai[MAX_N];
char str[MAX_N];

void insert(ll x)
{
    for (int i = 62; i >= 0; i--)
        if (x & (1LL << i))
        {
            if (base[i] == 0)
            {
                base[i] ^= x;
                break;
            }
            x ^= base[i];
        }
}

bool query(ll x)
{
    for (int i = 62; i >= 0; i--)
        if (x & (1LL << i))
            x ^= base[i];
    return x == 0;
}

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf("%lld", &ai[i]);
        scanf("%s", str + 1);
        for (int i = 0; i <= 62; i++)
            base[i] = 0;
        bool flag = false;
        for (int i = n; i >= 1; i--)
        {
            if (str[i] == '0')
                insert(ai[i]);
            else if (!query(ai[i]))
            {
                flag = true;
                break;
            }
        }
        puts(flag ? "1" : "0");
    }
    return 0;
}
```

## B - 01 Unbalanced

Why do I feel this problem is also really <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">stupid</span>, and yet I just couldn't figure it out... <span class="slang" data-zh="日了" data-gloss="rì le, “f***ed”. A vulgar exclamation of frustration, like “damn it” or “f***”.">F***</span>.

Essentially, we want a way to assign values to the ?s that minimizes the range of the prefix sums. Then we can use the <span class="slang" data-zh="套路" data-gloss="tàolù, “routine, set pattern”. OI slang for a standard, well-known trick that works on a whole class of problems.">standard trick</span> of enumerating the minimum of the whole prefix sum and making adjustments on top of that minimum. Enumerate $M$ as the minimum; then we can use a suffix minimum to check whether adjusting a ? would push a later minimum element out of bounds, that is, below the minimum. This is $\Theta(n^2)$.

But we can see this: what we're computing is a range, so our minimum doesn't have to be very small, and it turns out that values near the global minimum already do the job. (The range floats: let the global minimum be $X$; going down from $X - 2$, there will be $-1$s or $1$s that can't help shrink the range.)

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

using namespace std;

const int MAX_N = 1e6 + 200;

int n, pre[MAX_N], suf[MAX_N], ans = 0x7fffffff;
char str[MAX_N];

void prepare()
{
    for (int i = 1; i <= n; i++)
        pre[i] = pre[i - 1] + (str[i] == '0' ? -1 : 1);
    suf[n + 1] = MAX_N;
    for (int i = n; i >= 0; i--)
        suf[i] = min(suf[i + 1], pre[i]);
}

void solve(int lbound)
{
    int bias = 0, upper = 0;
    for (int i = 1; i <= n; i++)
    {
        if (str[i] == '?' && suf[i] - 2 - bias >= lbound)
            bias += 2;
        pre[i] -= bias, upper = max(upper, pre[i]);
    }
    ans = min(ans, upper - lbound);
}

int main()
{
    scanf("%s", str + 1), n = strlen(str + 1);
    prepare(), solve(suf[0]);
    prepare(), solve(suf[0] - 1);
    printf("%d\n", ans);
    return 0;
}
```
