We need to find minimum number of operations (row or column paints) to achieve at least N black squares. Initially white. Painting a row paints all squares in that row black. Painting a column paints all squares in that column black. Overpaints: if you paint a row then later a column, the intersection square gets painted twice, but remains black. So final black squares are union of painted rows and painted columns. If we paint r rows and c columns, total black squares = rW + cH - rc (because the intersection squares counted twice). Because the rows paint W squares per row, columns paint H squares per column, and overlap is rc squares. So total = rW + cH - rc.
We need rW + cH - rc >= N, with 0 <= r <= H, 0 <= c <= W, integer, and we want minimize total operations k = r + c.
Since H and W <= 100, we can brute force over all possible r (0..H) and c (0..W). For each r, we can compute min c needed, and take min r+c. That's trivial. But we can also solve analytically.
However, we have to guarantee existence. The constraints guarantee that N <= H*W, so it's possible.
Brute force is fine because H, W <= 100, at most 101*101 = 10201 combos. So we can loop.
But we need to find minimal r+c such that rW + cH - rc >= N.
We can also compute minimal c given r: Need c*(H - r) >= N - r*W? Actually rearrange:
rW + cH - rc >= N
=> c(H - r) >= N - rW
If H - r = 0, i.e., r = H, then we have painted all rows, so total black = HW regardless of c (since all rows painted, columns add nothing). So if r=H, condition is satisfied if HW >= N, which is guaranteed. Then c can be 0. So for r=H, we take c=0. Similarly, if c=W, total black = H*W. But we need to minimize.
Given r, we need c such that if H - r > 0, then c >= ceil( (N - rW) / (H - r) ), with the constraint that 0 <= c <= W. Also if N - rW <= 0, then c = 0 works. Also note that if (N - rW) > 0, then we need at least that many columns. But we must also ensure that c <= W; if computed c > W, then this r is not feasible? Actually if computed c > W, we cannot achieve N with that r because even painting all columns won't be enough. But could r + c > H+W? However it's possible that painting all columns yields total black = rW + WH - rW = WH = HW (since c=W, total = rW + WH - rW = WH). So if N <= HW, painting all columns (c=W) gives total = HW, which is >= N. So for any r, painting all columns yields total = HW >= N, so c=W always works. But if computed c > W, then we could still set c=W to satisfy. Actually the inequality: rW + cH - rc >= N. For c=W, total = rW + WH - rW = WH = HW, which is >= N. So any r is feasible with c=W. Similarly any c is feasible with r=H. So we don't need to worry about infeasibility. So for each r, we can compute minimal c as max(0, ceil((N - rW) / (H - r))) but only if H - r > 0 and (N - rW) > 0; else c = 0. And also cap c at W? Actually we are looking for minimal c that satisfies inequality. The minimal c may be less than or equal to W; if the computed minimal c is > W, then the minimal c is not > W because we can just set c=W and it satisfies. But does c=W give total >= N? Yes always. So the minimal c for that r is at most W. However if computed minimal c > W, that means that even with c = W, the inequality may not hold? Wait, we must check: if rW + WH - rW = HW >= N. So it's always >= N. So if computed minimal c > W, that would mean that the inequality requires more columns than available to reach N. But we already argued that c=W gives total = HW which is at least N. So if computed c > W, then that must be because we used the formula c >= ceil((N - rW)/(H - r)). But if (N - rW)/(H - r) > W, then N - rW > W(H - r) => N > rW + WH - rW = HW. That would imply N > HW, which is impossible because N <= HW. So it cannot happen. So indeed computed minimal c will always be <= W. Let's verify: We need to show that if H - r > 0, then (N - rW) <= W(H - r). Since N <= HW, we have N - rW <= HW - rW = W(H - r). So indeed the required c <= W. Similarly if N - rW <=0, then c=0 works.
Thus we can compute minimal c for each r as:
if r == H:
c_min = 0
else:
needed = max(0, N - r*W)
c_min = ceil(needed / (H - r)) (integer division careful)
if needed % (H - r) == 0: c_min = needed // (H - r) else = needed // (H - r) + 1
But if needed == 0 then c_min = 0.
Alternatively, loop over all c as well. Simpler: double loop.
Given constraints are tiny, we can just brute force over r and c and check condition, keep min r+c. That is easiest and avoids off-by-one.
Implementation:
- Read H, W, N.
- Initialize min_ops = H+W (max possible, but we can find lower)
- For r in range(0, H+1):
For c in range(0, W+1):
black = rW + cH - r*c
if black >= N:
min_ops = min(min_ops, r+c)
- Print min_ops.
But careful: r+c counts operations; but painting same row or column multiple times? Shouldn't do that because it's wasteful. So we consider distinct rows/columns. But we assume we paint each row at most once. So r and c are number of distinct rows/columns painted. Since painting a row twice doesn't add new black squares, we can assume we paint each at most once. So r from 0 to H, c from 0 to W.
Complexity O(H*W) = 10^4, fine.
Check examples:
Example1: H=3, W=7, N=10. Let's compute: For r=2, c=0 => black=2*7=14 >=10, ops=2. So min=2. Indeed output 2.
Example2: H=14, W=12, N=112. Let's brute: r? We'll compute later. Output 8.
Example3: H=2, W=100, N=200. r=2,c=0 => black=200 => ops=2, output 2.
Thus simple.
But note: Could there be a scenario where painting rows and columns in a different order matters? No, only counts.
Thus answer is straightforward.
But maybe there is nuance: The operations are "paint all squares in that row black" and "paint all squares in that column black". So if you paint a row and then later a column, the intersection gets painted again but remains black; but it's same result. So indeed only matters which rows and which columns are painted.
Thus solution: min_{0<=r<=H,0<=c<=W} (r+c) s.t. rW + cH - r*c >= N.
We can solve via double loop.
Edge cases: N=0? But constraints N>=1. N=HW? Then we need to paint all squares. Options: paint all rows (H ops) or all columns (W ops) or combination: r=H (H ops), c=W (W ops), or maybe r=H-1 and c=W? That yields HW - (H-1)? Actually r=H-1 and c=W: black = (H-1)W + WH - (H-1)W = HW. So r=H-1, c=W gives ops = H-1+W, which might be > min(H,W). So min would be min(H,W) (since painting all rows or all columns yields HW). Actually painting all rows gives H ops, all columns gives W ops. So min is min(H,W). So correct.
Double loop will capture that.
Thus code is simple.
Now, to be efficient we could break early, but not needed.
Implementation in Python:
def solve():
import sys
H = int(sys.stdin.readline().strip())
W = int(sys.stdin.readline().strip())
N = int(sys.stdin.readline().strip())
min_ops = H+W
for r in range(H+1):
for c in range(W+1):
black = rW + cH - r*c
if black >= N:
if r+c < min_ops:
min_ops = r+c
print(min_ops)
Works.
But we can also reduce loops: For each r, compute c_min as described and then r + c_min. That is O(H). That's also simple.
Let's do the direct double loop, it's fine.
Testing:
Example1: H=3,W=7,N=10. min_ops initially 10. r=0, c=2 => black=0+2*3-0=6 <10; c=3 => 9<10; c=4 =>12>=10 ops=4; r=1,c=2=> black=7+6-2=11>=10 ops=3; r=1,c=1=>7+3-1=9<10; r=2,c=0=>14>=10 ops=2; r=2,c=1=>14+3-2=15>=10 ops=3; So min=2.
Example2: H=14,W=12,N=112. Let's test with double loop quickly: r=8,c=0 => black=812=96 <112; r=8,c=1 => 96+14-8=102; r=8,c=2=>96+28-16=108; r=8,c=3=>96+42-24=114 >=112 ops=11; r=7,c=2=>84+28-14=98; r=7,c=3=>84+42-21=105; r=7,c=4=>84+56-28=112 ops=11; r=6,c=4=>72+56-24=104; r=6,c=5=>72+70-30=112 ops=11; r=5,c=5=>60+70-25=105; r=5,c=6=>60+84-30=114 ops=11; r=4,c=7=>48+98-28=118 ops=11; r=3,c=8=>36+112-24=124 ops=11; r=2,c=9=>24+126-18=132 ops=11; r=1,c=10=>12+140-10=142 ops=11; r=0,c=10=>0+140-0=140 ops=10; r=0,c=9=>126 ops=9; Wait, r=0,c=9 => black=914=126 >=112 ops=9. r=0,c=8 =>112? 8*14=112 exactly, ops=8. So c=8, r=0 gives ops=8, black=112. So min=8. That matches output.
Example3: H=2,W=100,N=200 => r=2,c=0 ops=2 => black=200. r=0,c=2 => black=22? Actually c=2: black=22=4? Wait: W=100, H=2. For r=0,c=2: black = 0 + 22 -0 =4? That's wrong: careful: cH = 22=4, but we need 200. Actually we need c=100? Let's compute: c columns painted, each column paints H=2 squares, so total from columns = c2. So to get 200, need c=100. That's 100 ops. So min is 2. So answer 2.
Thus solution works.
Now, we need to write answer. Should we consider reading input? Yes.
Edge cases: H=100, W=100, N=1 => we need min ops=1 because either paint one row or one column yields 100 black squares >=1. So r=1,c=0 or r=0,c=1 => ops=1. So okay.
Thus final code.
But maybe there's a nuance: The operations are performed sequentially; but if you paint a row and then a column, the intersection is painted twice, but the number of black squares after both is as computed. So it's fine.
Thus answer is trivial.
But since the problem is from AtCoder or something? Actually it's from AtCoder Beginner Contest? It may require thinking about minimal operations, perhaps a more elegant solution: min_{r} (r + ceil((N - rW)/(H - r))) for r from 0 to H. But double loop is fine given constraints.
We'll produce solution in Python.
We should note that H and W up to 100, so double loop is fine.
Now, I'll produce final answer.
But wait: In the double loop we must ensure we consider the case of painting 0 rows and 0 columns? That yields black=0, which may not satisfy N. So fine.
Now produce final answer.
Implementation:
def main():
import sys
H = int(sys.stdin.readline())
W = int(sys.stdin.readline())
N = int(sys.stdin.readline())
ans = H + W # upper bound
for r in range(H+1):
for c in range(W+1):
black = rW + cH - r*c
if black >= N:
ans = min(ans, r+c)
print(ans)
if name == "main":
main()
Testing with sample inputs.
First: 3,7,10 -> ans=2.
Second: 14,12,112 -> ans=8.
Third: 2,100,200 -> ans=2.
Works.
Thus final solution.
But the problem might require reading from standard input with spaces? Actually format: H on first line, W on second, N on third. So we read three lines.
Ok.
Now, I can provide explanation.
Thus answer done. ```python
import sys
def main() -> None:
H = int(sys.stdin.readline())
W = int(sys.stdin.readline())
N = int(sys.stdin.readline())
# Upper bound: paint all rows and all columns (never needed)
ans = H + W
for r in range(H + 1):
for c in range(W + 1):
black = r * W + c * H - r * c
if black >= N:
ans = min(ans, r + c)
print(ans)
if name == "main":
main()