Answer to Question #280897 in Python for Rahul Sigh

Question #280897

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.


Input format

•  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


1 T 1000001 N 1000000


It is guaranteed that the sum of N over T test cases does not exceed 1e6.


1
Expert's answer
2021-12-20T10:02:18-0500
# 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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS