D - AtCoder Wallpaper 解説 by evima
Another ImplementationSimilar to the official editorial, we focus on the repetition of the \(4 \times 2\) rectangle.
First, we add an appropriate value (such as \(10^9\)) to all coordinates, ensuring that \(0 \leq A, 0 \leq C\) without changing the area we want to calculate.
Under this condition, if we write the answer to the problem as \(f(A, B, C, D)\), it can be obtained as \(f(0, 0, C, D) - f(0, 0, C, B) - f(0, 0, A, D) + f(0, 0, A, B)\) (keyword: two-dimensional cumulative sum).
Next, we need to find \(f(0, 0, A, B)\). By dividing the area with the lines \(x = 4 \cdot \lfloor \frac{A}{4} \rfloor\) and \(y = 2 \cdot \lfloor \frac{B}{2} \rfloor\), we can calculate it as a repetition of the same rectangle pattern. It is useful to manually construct the two-dimensional cumulative sum of the \(4 \times 2\) rectangle pattern.
Sample Implementation: (Python)
def f(y, x):
a = [[0, 0, 0, 0, 0], [0, 2, 3, 3, 4], [0, 3, 6, 7, 8]]
sub1 = (y // 2) * (x // 4) * a[2][4]
sub2 = (y // 2) * a[2][x % 4]
sub3 = (x // 4) * a[y % 2][4]
sub4 = a[y % 2][x % 4]
return sub1 + sub2 + sub3 + sub4
M = 10**9
A, B, C, D = map(lambda x: int(x) + M, input().split())
print(f(D, C) - f(D, A) - f(B, C) + f(B, A))
投稿日時:
最終更新: