---
id: 721
lang: en
translation_of: index.md
source_sha256: c12e0dff2b0c8c22e2c501d215fa6fa860028aaec71c7cb1df72146970c0b16e
title: "[Fortuna OJ] Feb 16th – Group B Problem-Solving Report"
description: "Notes on four problems: two dismissed as trivial, a minimum prefix-sum difference mod P found with a set, and a max Manhattan distance via sign bitmasks."
tags: ["OI","Ad Hoc","Problem-Solving Report","Bitmask","Prefix Sums"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes: []
---

## A - Binary & B - Chess

<span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">Idiotic</span> problems, no analysis.

## C - Sum

This problem is really interesting (after careful parameter tuning, a randomized program can get 90 points), and the intended solution is really clever. We can reduce the problem to finding $min\{ S_{i,j} \mod P, K \leq S_{i,j} \mod P\}$. We first spend $O(n)$ time precomputing the prefix sums, then we process the prefix sums in reverse order, inserting them into a set from back to front. While inserting into the set, we binary search for the two target optimal solutions $s[i]+k$ and $s[i]+k-p$, then just record the answer. Really clever.

```cpp
// C.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 100010;
int n, k, p, ans = 0x7f7f7f7f, s[MAX_N];
set<int> st;
int main()
{
    scanf("%d%d%d", &n, &k, &p);
    for (int i = 1; i <= n; i++)
        scanf("%d", &s[i]), (s[i] += s[i - 1]) %= p;
    set<int>::iterator it;
    st.insert(s[n]);
    for (int i = n - 1; i >= 1; i--)
    {
        it = st.lower_bound(s[i] + k);
        if (it != st.end())
            ans = min(*it - s[i], ans);
        it = st.lower_bound(s[i] + k - p);
        if (it != st.end() && *it < s[i])
            ans = min(*it - s[i] + p, ans);
        st.insert(s[i]);
    }
    printf("%d", ans);
    return 0;
}
```

## D - Jail

> Oh, an <span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">idiotic</span> problem. —crazydave

This one counts as <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.">a standard trick</span>, I guess: use a bitmask to enumerate the sign state of each dimension, find the maximum and the minimum, and take the maximum minus the minimum (the minimum stands for the Manhattan contribution of the opposite state). That’s it.

```cpp
// D.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000010;
int n, d, info[MAX_N][6], st[10], ans;
void calc(int stat)
{
    int mn = (1 << 29), mx = -mn;
    for (int i = 1; i <= d; i++, stat >>= 1)
        st[i] = stat & 1;
    for (int i = 1; i <= n; i++)
    {
        int acc = 0;
        for (int j = 1; j <= d; j++)
            acc += (st[j] == 1) ? info[i][j] : -info[i][j];
        if (acc != 0)
            mn = min(mn, acc), mx = max(mx, acc);
    }
    ans = max(mx - mn, ans);
}
int main()
{
    scanf("%d%d", &n, &d);
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= d; j++)
            scanf("%d", &info[i][j]);
    for (int i = 0; i < (1 << d); i++)
        calc(i);
    printf("%d", ans);
    return 0;
}
```
