---
id: 821
lang: en
translation_of: index.md
source_sha256: b1cb05932e32d177535f8ae9025790d7d4f606bb797384df24a780927b662b07
title: "U63113: Invasion - a.k.a. “XG's Math Problem”"
description: "A really nice counting DP set by XG_Zepto: count permutations by the position of the i-th number, then simplify and optimize with prefix sums."
tags: ["OI","Godlike","Counting","Counting DP"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "「XG 的数学题」 in the title is a quoted alternate name, not a contest tag, so it is rendered with curly quotes rather than square brackets."
  - "换弹次数 (number of magazine changes) rendered as “number of reloads”."
  - "神仙组长 rendered as “godlike group leader”; only 神仙 is chipped, since 组长 is a plain word."
---

## ~~Background~~

~~As everyone knows, XG_Zepto is <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">a god</span> from FZOI. He thought the current contests were too easy, so he used his Minesweeper time to set a math problem for kal0rona. kal0rona is way too <span class="slang" data-zh="菜" data-gloss="cài, “vegetable”. Slang for being bad at something; weak.">weak</span> to understand the problem or solve it, so now he's turning to you for help.~~

## Approach

A really nice counting DP, set by our <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span> group leader XG_Zepto. First, let $f[i][j]$ be the number of valid permutations of length $i$ with $j$ reloads. Then, consider enumerating, for the $i$-th number, the position $k$ where it appears. The numbers after position $k$ make no special contribution (the contribution is $(i-k)!$), while for the numbers before it, we can enumerate the previous segment to transition: there are ${k-1}\choose{i-1}$ ways to choose the numbers and $f[i-1][j-1$ permutations, and we just multiply them together, that is:

$$
F[i][j] = \sum_{k = 1}^{i}f[k-1][j-1]* {{k-1} \choose {i-1}} *(i-1)!
$$

This can be simplified and optimized with prefix sums:

$$
F[i][j] = (i-1)!\sum_{k=1}^{i}\frac{f[k-1][j-1]}{(k-1)!}
$$

## Code

```cpp
// U63113.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 5050, mod = 998244353;
ll f[MAX_N], prefix[MAX_N], level[MAX_N], inv[MAX_N], n, m;
ll quick_pow(ll bas, ll tim)
{
    ll ans = 1;
    while (tim)
    {
        if (tim & 1)
            ans = ans * bas % mod;
        bas = bas * bas % mod;
        tim >>= 1;
    }
    return ans;
}
int main()
{
    scanf("%lld%lld", &n, &m);
    level[0] = 1, inv[0] = 1;
    for (int i = 1; i <= n; i++)
        level[i] = level[i - 1] * i % mod;
    inv[n] = quick_pow(level[n], mod - 2);
    for (int i = n; i >= 1; i--)
        inv[i - 1] = inv[i] * i % mod;
    for (int i = 0; i <= n; i++)
        prefix[i] = 1;
    for (int j = 1; j <= m; j++)
    {
        for (int i = j; i <= n; i++)
            f[i] = prefix[i - 1] * level[i - 1] % mod;
        for (int i = j; i <= n; i++)
            prefix[i] = f[i] * inv[i] % mod;
        for (int i = j + 1; i <= n; i++)
            prefix[i] = (prefix[i] + prefix[i - 1]) % mod;
    }
    printf("%lld", f[n]);
    return 0;
}
```
