[PYTHON] Solve HHKB2020 D --Squares by pushing

Commentary

Click here for a link to the problem. HHKB2020 D - Squares

Way of thinking

(The blue square is called $ S_A $ and the red square is called $ S_B $.)

After deciding how to place it in the y-axis direction, consider how to place it in the x-axis direction.

At this time, the condition that the two squares do not overlap is that either of the following is satisfied. (1) There is no overlap in the y-axis direction (the x-axis direction can be free) (2) When there is overlap in the y-axis direction, there is no overlap in the x-axis direction.

With these two patterns, you can enumerate all the placements that satisfy the theme without omission. (I realize that it was easier to think about the complementary events ...)

(1) When there is no overlap in the y-axis direction

First, consider how many ways to place them in the y-axis direction so that they do not overlap **. Once, set $ (y-coordinate of S_A) <(y-coordinate of S_B) $.

Hereafter, set $ d = N --A --B $. //////////////////////// When the y coordinate of $ S_A $ is $ [0, a] $ The y coordinate of $ S_B $ is $ [a, a + b], [a + 1, a + b + 1], ... [nb, n] $ $ n- (a + b) + 1 = d +1 $ street.

When the y coordinate of $ S_A $ is $ [1, a + 1] $ The y coordinate of $ S_B $ is $ d $ of $ [a + 1, a + b + 1], ... [n-b, n] $.

...

When the y coordinate of $ S_A $ is $ [n-a-b, n-b] $ The y coordinate of $ S_B $ is $ 1 $ of $ [n-b, n] $. /////////////////////////

Therefore, when $ (y coordinate of S_A) <(y coordinate of S_B) $, There are $ (d + 1) + d + ... + 1 = (d + 1) (d + 2) / 2 $ ways to place them in the y-axis direction so that they do not overlap.

The same applies when $ (y coordinate of S_A)> (y coordinate of S_B) $, so There are a total of $ 2 (d + 1) (d + 2) / 2 = (d + 1) (d + 2) $ for placement in the y-axis direction so that they do not overlap.

When there is no overlap in the y-axis direction, $ S_A $ and $ S_B $ do not overlap in any way in the x-axis direction. Since $ S_A $ and $ S_B $ are placed in the x-axis direction in $ (n-a + 1) $ and $ (n-b + 1) $, respectively. The way to put (1) is $ (d + 1) (d + 2) (n-a + 1) (n-b + 1) $.

(2) When there is overlap in the y-axis direction

First, consider how many ways to place in the y-axis direction, such as ** overlapping **. I already know how to put it when there is an overlap in $ (d + 1) (d + 2) $, so You can subtract this value from the entire placement in the y-axis direction. That is, $ (n-a + 1) (n-b + 1)-(d + 1) (d + 2) $.

Since there is overlap in the y-axis direction, it is necessary to place them so that they do not overlap in the x-axis direction. In this problem, the conditions are the same for both the x-axis direction and the y-axis direction, so $ (placement that does not overlap in the x-axis direction) = (placement that does not overlap in the y-axis direction) $. In other words, the way of placing them so that they do not overlap in the x-axis direction is as $ (d + 1) (d + 2) $ by diverting the result of (1).

From the above, how to place (2) $ \ {(n-a + 1) (n-b + 1)-(d + 1) (d + 2) \} (d + 1) (d + 2) $ That's right.

The rest is done by adding the results of (1) and (2).

AC cord

MOD = 10**9+7
for _ in range(int(input())):
    n,a,b = map(int, input().split())
    if a+b > n:
        print(0)
        continue

    d = n-a-b
    no_overlap = (d+1)*(d+2)
    a_free = n-a+1
    b_free = n-b+1

    ans = no_overlap*a_free*b_free + (a_free*b_free-no_overlap)*no_overlap
    print(ans%MOD)

Recommended Posts

Solve HHKB2020 D --Squares by pushing
Solve ABC175 D in Python