Manasa and Calculations

  • + 0 comments

    Can anyone please tell me how to improve my code efficiency? I wrote this but its showing runtime error for most testcases. It is in python 3.

    from fractions import gcd
    n = int(input())
    A = 1
    B = 1
    for _ in range(n):
        p1,b1,a1 = map(int, input().split())
        A *= pow(p1,a1)
        B *= pow(p1,b1)
    num = 0
    for j in range(B,A+1,B):
        for k in range(B,A+1,B):
            if j <= k and gcd(j,k) == B and j*k == A*B:
                num += j+k
    print(num%(pow(10,9)+7))