G - Groups Editorial by polarity


Let \(DP[i][j]\) be the number of ways to have the first i numbers into j groups.

We can have the following formula:

\(DP[i][j] = DP[i - 1][j - 1] + DP[i - 1][j]*(j -\lfloor (i - 1)/m \rfloor)\)

To explain it verbally, it means that we can either let the next element be a new group itself, or to put it in an existed group that don’t have the number with the same \(\bmod m\).

The base case will be \(DP[0][0] = 1\).

The answer for \(k=i\) will be \(DP[n][i]\).

Submission: https://atcoder.jp/contests/abc217/submissions/25645524

posted:
last update: