---
id: 1950
lang: en
translation_of: index.md
source_sha256: bd8c92b8a299c26ebc2a92cfb3e3573a479542f6e25fcbeac8a2cc84c5b7ed3d
title: "[Fortuna OJ] Jan 9 – Group A Problem-Solving Report"
description: "Problem-solving report for three problems: a 2D partial order, a binomial coefficient modulo a composite via Lucas and CRT, and a digit DP."
tags: ["Dynamic Programming","OI","Digit DP","2D Partial Order","Chinese Remainder Theorem","Multiplicative Inverse"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "回文数 (literally “palindromic number”) rendered as “reversal” throughout section C, including inside the \\text{} of the display formula, because the author uses it for the digit-reversed number."
  - "收历史作业 rendered literally as “Collecting History Homework”; 历史作业 could also mean “past homework”."
  - "The empty \\text{} in the display formula replaces 的数的个数, whose meaning (“count of numbers”) moved into the first \\text{} to read in English word order."
---

Feels like there is still a lot of stuff I can’t remember, <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>.

<!--more-->

## A - Collecting History Homework

<span class="slang" data-zh="傻逼" data-gloss="shǎbī. A vulgar insult, roughly “idiot” or “stupid”.">Idiotic</span> 2D partial order, <span class="slang" data-zh="切" data-gloss="qiē, “to cut”. OI slang for solving a problem, i.e. getting it accepted.">cracked it</span> 8 minutes into the contest (why weren’t there <span class="slang" data-zh="送分题" data-gloss="sòngfēn tí, “point-giving problem”. A problem so easy it is basically free points.">free-points problems</span> like this over the summer?)

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

using namespace std;

const int MAX_N = 2e5 + 200;

int n, m, k, nodes[MAX_N], upper;
vector<int> mp;

struct point
{
    int x, y, c;
    bool operator<(const point &rhs) const { return x < rhs.x || (x == rhs.x && y < rhs.y) || (x == rhs.x && y == rhs.y && c > rhs.c); }
} pts[MAX_N];

int ripe(int x) { return lower_bound(mp.begin(), mp.end(), x) - mp.begin() + 1; }

inline int lowbit(int x) { return x & (-x); }

void update(int x, int d)
{
    for (; x <= upper; x += lowbit(x))
        nodes[x] = max(nodes[x], d);
}

int query(int x, int ret = 0)
{
    for (; x; x -= lowbit(x))
        ret = max(ret, nodes[x]);
    return ret;
}

int main()
{
    scanf("%d%d%d", &n, &m, &k);
    for (int i = 1; i <= k; i++)
    {
        scanf("%d%d%d", &pts[i].x, &pts[i].y, &pts[i].c);
        mp.push_back(pts[i].x), mp.push_back(pts[i].y);
    }
    pts[++k] = point{n - 1, m - 1, -1}, mp.push_back(n - 1), mp.push_back(m - 1);
    sort(mp.begin(), mp.end()), mp.erase(unique(mp.begin(), mp.end()), mp.end());
    upper = mp.size();
    for (int i = 1; i <= k; i++)
        pts[i].x = ripe(pts[i].x), pts[i].y = ripe(pts[i].y);
    sort(pts + 1, pts + 1 + k);
    int ans = 0;
    for (int i = 1; i <= k; i++)
    {
        int dp = query(pts[i].y) + pts[i].c;
        if (pts[i].c == -1)
            ans = ++dp;
        update(pts[i].y, dp);
    }
    printf("%d\n", ans);
    return 0;
}
```

## B - Handing Out Bonuses

After some observation, the answer turns out to be very obvious: $n + m \choose m$. Looking at the moduli, $60\%$ can be done with Lucas, and then I got stuck (purely because I couldn’t remember how to write CRT).

First, the $80\%$ is easy to write: just compute with Lucas under the two moduli, then merge with CRT. Actually the intended solution is pretty much the same, Lucas several times and then merge. But the intended solution has one special thing: there exist $c_i \geq 1$. So you can’t compute directly with multiplicative inverses. However, we notice that a multiplicative inverse exists if and only if the two numbers are coprime. So when computing the inverse we can pull $p_i$ out, and let the coprime part find its inverse with exgcd.

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

using namespace std;

const int MAX_N = 1e5 + 200;

int n, m, p, primes[MAX_N], cnt[MAX_N], ptot, cmod, pic[MAX_N], ai[MAX_N];

int exgcd(int a, int b, int &x, int &y)
{
    if (b == 0)
    {
        x = 1, y = 0;
        return a;
    }
    int d = exgcd(b, a % b, x, y), t = x;
    x = y, y = t - (a / b) * y;
    return d;
}

int getInv(int a)
{
    int x, y;
    exgcd(a, cmod, x, y);
    return (x + cmod) % cmod;
}

int quick_pow(int bas, int tim)
{
    if (tim < 0)
        return quick_pow(getInv(bas), -tim);
    int ret = 1;
    while (tim)
    {
        if (tim & 1)
            ret = 1LL * ret * bas % cmod;
        bas = 1LL * bas * bas % cmod;
        tim >>= 1;
    }
    return ret;
}

int fac(int x, int mod)
{
    int ret = 1;
    for (int i = 1; i <= x; i++)
        if (i % mod != 0)
            ret = 1LL * ret * i % cmod;
    return ret;
}

int fac(int x, int mod, int &tot)
{
    // lucas;
    if (x < mod)
        return fac(x, mod);
    int seg = x / cmod, rem = x % cmod;
    int res = 1LL * quick_pow(fac(cmod - 1, mod), seg) * fac(rem, mod) % cmod;
    tot += x / mod;
    res = 1LL * res * fac(x / mod, mod, tot) % cmod;
    return res;
}

int binomial(int n_, int k_, int mod)
{
    int a = 0, b = 0, c = 0, res = 1;
    res = 1LL * res * fac(n_, mod, a) % cmod * getInv(fac(k_, mod, b)) % cmod * getInv(fac(n_ - k_, mod, c)) % cmod;
    return 1LL * res * quick_pow(mod, a - (b + c)) % cmod;
}

void factorize(int tmp)
{
    for (int i = 2; 1LL * i * i <= tmp; i++)
        if (tmp % i == 0)
        {
            primes[++ptot] = i;
            while (tmp % i == 0)
                cnt[ptot]++, tmp /= i;
        }
    if (tmp > 1)
        primes[++ptot] = tmp, cnt[ptot] = 1;
}

int main()
{
    scanf("%d%d%d", &n, &m, &p), n += m;
    factorize(p);
    for (int i = 1; i <= ptot; i++)
    {
        cmod = 0x7fffffff, cmod = pic[i] = quick_pow(primes[i], cnt[i]);
        ai[i] = binomial(n, m, primes[i]);
    }
    cmod = 0x7fffffff;
    // begins to merge;
    for (int i = 2; i <= ptot; i++)
    {
        int x, y;
        exgcd(pic[i - 1], pic[i], x, y);
        pic[i] *= pic[i - 1];
        ai[i] = ((1LL * x * (ai[i] - ai[i - 1]) * pic[i - 1] % pic[i] + ai[i - 1]) % pic[i] + pic[i]) % pic[i];
    }
    printf("%d\n", ai[ptot]);
    return 0;
}
```

## C - A Big <span class="slang" data-zh="水题" data-gloss="shuǐtí, “water problem”. An easy problem.">Freebie</span>

~~A big <span class="slang" data-zh="毒瘤" data-gloss="dúliú, “tumor”. A problem (or problem setter) that is painfully hard, tedious, or full of traps.">nasty problem</span>~~

You can tell it’s digit DP, but I just couldn’t get it written at all. The answer must be:

$$
\text{ans} = m - \frac{\text{count with reversal} \leq m \text{} - \text{count equal to own reversal}}{2}
$$

So we can consider the state $dp[i][0/1][0/1]$, meaning, up to digit $i$, whether it equals $m$ and whether it equals the reversal of $m$. Then the count of those less than $m$ is clearly $dp[len][0][0]+dp[len][1][0]$. For the count of numbers equal to their own reversal, no doubt we only need the first half to be less, that is, $dp[len >> 1][0][0] + {}$<wbr>$dp[len >> 1][0][1] + {}$<wbr>$dp[len >> 1][1][0]$.

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

using namespace std;

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

int n, len, m, digits[MAX_N * MAX_N], dp[MAX_N * MAX_N][2][2];
char str[MAX_N * MAX_N];

int quick_pow(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;
}

const int inv2 = quick_pow(2, mod - 2);

int main()
{
    scanf("%d%s", &n, str + 1), len = n * n;
    for (int i = 1; i <= len; i++)
        digits[i] = str[i] - '0', m = (1LL * m * 10 % mod + digits[i]) % mod;
    int last_digit;
    dp[0][1][0] = 1;
    for (int i = 0; i <= len - 1; i++)
        for (int bit = 0; bit <= 1; bit++)
        {
            last_digit = bit ? digits[i + 1] : 9;
            for (int k = 0; k <= last_digit; k++)
            {
                dp[i + 1][bit && (k == last_digit)][(k <= digits[len - i]) ? 0 : 1] =
                    (dp[i + 1][bit && (k == last_digit)][(k <= digits[len - i]) ? 0 : 1] + dp[i][bit][0]) % mod;
                dp[i + 1][bit && (k == last_digit)][(k < digits[len - i]) ? 0 : 1] =
                    (dp[i + 1][bit && (k == last_digit)][(k < digits[len - i]) ? 0 : 1] + dp[i][bit][1]) % mod;
            }
        }
    int firstTerm = (0LL + dp[len][0][0] + dp[len][1][0]) % mod;
    int secondTerm = (0LL + dp[len >> 1][0][0] + dp[len >> 1][0][1] + dp[len >> 1][1][0]) % mod;
    int ans = (0LL + m + mod - 1LL * (firstTerm + mod - secondTerm) % mod * inv2 % mod) % mod;
    printf("%d\n", ans);
    return 0;
}
```
