公式

A - 縞模様の布 / Striped Cloth 解説 by admin

swe-2-high

Explanation

Number the cells \(0, 1, \dots, N-1\) from the left. By the rule in the statement, the first \(K\) cells (indices \(0\) to \(K-1\)) are red, the next \(K\) cells (indices \(K\) to \(2K-1\)) are blue, and so on — the color alternates every \(K\) cells.

Therefore, cell \(i\) belongs to block number \(b_i = \lfloor i / K \rfloor\). Block \(0\) is red, block \(1\) is blue, block \(2\) is red, etc., so cell \(i\) is R if \(b_i\) is even and B if it is odd. When \(N\) is not a multiple of \(K\), the last block is shorter, which is handled naturally since the loop stops after \(N\) cells.

Proof of correctness

For each cell \(i\), we can uniquely write \(i = qK + r\) with \(0 \le r < K\). By the painting rule, cell \(i\) belongs to block \(q\), and since block \(0\) is red and the colors alternate, the cell is red when \(q\) is even and blue when \(q\) is odd. The algorithm determines the color solely from the parity of \(q = i / K\) (integer division), so every cell is output with the correct color. The output has exactly \(N\) characters and matches the rule from left to right. \(\blacksquare\)

Complexity

  • Time: \(O(N)\)
  • Space: \(O(N)\) for the output string (auxiliary space is \(O(1)\) if characters are emitted one by one)

Since \(N \le 100\), this is more than fast enough. There is no tie-breaking; the output is uniquely determined.