A - 縞模様の布 / Striped Cloth Editorial by admin Editorial for printing
gpt-6-astra-mediumOverview
We construct a string of length \(N\) by alternately assigning red and blue in blocks of \(K\) cells from left to right. By using integer division to find which stripe each cell belongs to, we can easily determine its color.
Analysis
Let us number the positions of the cells from left to right as \(0,1,\ldots,N-1\).
Since each stripe has a width of \(K\) cells, the stripe index to which cell at position \(i\) belongs is the quotient of \(i\) divided by \(K\). In Python, this can be computed using i // K.
If we also 0-index the stripes, the correspondence with the colors is as follows:
- Stripe index \(0,2,4,\ldots\): Red (
R) - Stripe index \(1,3,5,\ldots\): Blue (
B)
Therefore, we can simply choose R if (i // K) % 2 == 0, and B otherwise.
For example, when \(N=8,\ K=3\), the cells correspond to the following:
| Cell position \(i\) | \(0\) | \(1\) | \(2\) | \(3\) | \(4\) | \(5\) | \(6\) | \(7\) |
|---|---|---|---|---|---|---|---|---|
Stripe index i // K |
\(0\) | \(0\) | \(0\) | \(1\) | \(1\) | \(1\) | \(2\) | \(2\) |
| Color | R | R | R | B | B | B | R | R |
The output is RRRBBBRR.
Under the given constraints, a naive simulation that colors the cells \(1\) by \(1\) while toggling colors is also fast enough. However, if you append \(K\) characters at a time, the length may exceed \(N\) in the final stripe. Determining the color of each cell individually from position \(0\) to \(N-1\) naturally handles the final, shorter stripe without issue.
Algorithm
- Read \(N\) and \(K\).
- For each position \(i=0,1,\ldots,N-1\), perform the following:
- Calculate the stripe index using
i // K. - Choose
Rif the stripe index is even, andBif it is odd.
- Calculate the stripe index using
- Concatenate the chosen characters in order and output the result.
Complexity
- Time complexity: \(O(N)\)
We determine the color of each cell in constant time and construct a string of length \(N\). - Space complexity: \(O(N)\)
To store the resulting string of length \(N\) to be output.
Implementation Notes
Using
range(N)makes cell positions 0-indexed. If using 1-based indexing, the stripe index must be computed as(i - 1) // K.Use the integer division operator
//rather than/.Use
''.join(...)to concatenate the characters for each cell without separators.Even when \(N<K\),
i // Kevaluates to \(0\) for all positions, so all cells are correctly coloredR.Source Code
import sys
def main():
data = sys.stdin.read().split()
N = int(data[0])
K = int(data[1])
out = ''.join('R' if (i // K) % 2 == 0 else 'B' for i in range(N))
sys.stdout.write(out + '\n')
main()
This editorial was generated by gpt-6-astra-medium.
posted:
last update: