---
id: 961
lang: en
translation_of: index.md
source_sha256: 5506c1da5e68e5ebf54a21ef3768e32c8761df007c687536fc46b8b78a00a8e7
title: "[Fortuna OJ] Mar 8th - Group A Problem-Solving Report"
description: "Report on a coloring problem: a DP for the number of colors in one row, then a row-by-row DP counting colors shared between adjacent rows."
tags: []
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "涂色游戏 (the problem name) rendered as “Coloring Game”."
  - "消去g[i]中答案种类对g[i]的影响 is awkward in the original; rendered literally as “cancel out the effect that the kinds of colors in g[i] have on g[i]”."
---

It's the last day of the spring training camp, and sure enough, the contest <span class="slang" data-zh="崩了" data-gloss="bēng le, “collapsed”. Said of a contest: it went badly, ending in a poor result.">went to pieces</span> too.

## A - Coloring Game

How is this problem so <span class="slang" data-zh="神仙" data-gloss="shénxiān, “immortal”. Someone, or a problem or idea, so brilliant it seems superhuman.">godlike</span>?

Let's first consider the number of ways within a single row. We can define a state: $f[i][j]$ means that the first $i$ elements contain $j$ colors, and the formula is easy to derive:

$$
f[i][j] = f[i-1][j-1]*(p-(j-1))+f[i-1][j]*j
$$

Since the number of columns is fixed, we only need to keep the values for $i=m$, so we let $g[i]=f[m][i]$, that is, the number of ways for a single row to have $i$ colors. Next we think about designing a state to compute our answer: we can fix one row and then discuss the case of two rows placed together. So $dp[i][k]$ means the number of ways for the first $i$ rows, where the last row contains $k$ colors.

$$
dp[i][k] = dp[i-1][j]+\frac{g[k]}{ { p \choose k }}*\sum_{ x = max\{ j,k,q \} }^{ min\{ p,j+k \} } { { j \choose j+k-x }*{ p-j \choose x-j } }
$$

I know this formula is complicated, so let's explain it. First, the state $dp[i][k]$ means we have reached row $i$ and row $i$ has $k$ colors; we enumerate the previous row having $j$ colors, so we transition from $dp[i-1][j]$. Because here we enumerate the contribution of the color types to the answer separately, we have to cancel out the effect that the kinds of colors in $g[i]$ have on $g[i]$, to avoid double counting. Then we enumerate the two rows having $x$ colors in total. Because $j$ and $k$ may overlap, we need to enumerate this separately, and the bounds of $x$ are: $[max\{ j,k,q \},min\{ p, j+k \}]$. After that comes the <span class="slang" data-zh="喜闻乐见" data-gloss="xǐ wén lè jiàn, “glad to hear, happy to see”. An idiom for something everyone loves; used jokingly for a familiar, routine step.">ever-popular</span> separate enumeration of the two parts: ${ j \choose j+k-x }$ enumerates the part the two rows share, while ${ p-j \choose x-j }$ represents the part the two rows don't share. So, this way the answer can be counted.

Then the $f[i]$ in this problem can be handled with fast matrix exponentiation, lowering the complexity further.
