There is an N x N board. Rows and columns are numbered from 1 to N. Two persons P1 and P2 randomly select two numbers A and B in between 2 to 2 * N respectively. They independently paint each block whose sum of row and column numbers is a multiple of their chosen number. The beauty of the board is equal to the number of blocks painted at least once by either one of them. Note that even if they both painted the same block, it only counts once. Print the expected beauty of the board.
Rows and columns are numbered from 1 to N.
• The first line contains an integer T denoting the number of test cases.
• The first line of each test case contains an integer N denoting the size of the board.
Output format
For each test case, print the expected beauty of the board in a new line. Print your answer modulo 1e9+ 7.
Constraints
It is guaranteed that the sum of N over T test cases does not exceed 1e6.
# Python 3.9.5
import random
def paint_squares(P, N):
list_cor = []
for i in range(1, N+1):
for j in range(1, N+1):
if (i+j)%P == 0:
list_cor.append([i, j])
return list_cor
def get_random_numbers(N):
P1 = random.randrange(2, N*2, 1)
P2 = random.randrange(2, N*2, 1)
return P1, P2
def get_start_data():
T = int(input('Enter T: '))
N = int(input('Enter N: '))
return T, N
def main():
T, N = get_start_data()
i=1
while i<=T:
P1, P2 = get_random_numbers(N)
set1 = paint_squares(P1, N)
set2 = paint_squares(P2, N)
res = []
[res.append(x) for x in set1+set2 if x not in res]
print(f'Test case {i}: ')
print(f'P1={P1}', f'P2={P2}', f'Result: {len(res)}')
i += 1
if __name__ == '__main__':
main()
Comments
Leave a comment