Sum of all possible triplet products from given ranges | Python3 implementation to implement the above approach ; Function to find the value of power ( X , N ) % M ; Stores the value of ( X ^ N ) % M ; Calculate the value of power ( x , N ) % M ; If N is odd ; Update res ; Update x ; Update N ; Function to find modulo multiplicative inverse of X under modulo M ; Function to find the sum of all possible triplet products ( i * j * k ) ; Stores modulo multiplicative inverse of 8 ; Stores the sum of all possible values of ( i * j * k ) ; Update res ; Driver Code
M = 1000000007 NEW_LINE def power ( x , N ) : NEW_LINE INDENT global M NEW_LINE res = 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT res = ( res * x ) % M NEW_LINE DEDENT x = ( x * x ) % M NEW_LINE N = N >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def modinv ( X ) : NEW_LINE INDENT return power ( X , M - 2 ) NEW_LINE DEDENT def findTripleSum ( A , B , C ) : NEW_LINE INDENT global M NEW_LINE MMI = modinv ( 8 ) NEW_LINE res = 0 NEW_LINE res = ( ( ( ( A % M * ( A + 1 ) % M ) % M * ( B % M * ( B + 1 ) % M ) % M ) % M * ( C % M * ( C + 1 ) % M ) % M ) % M * MMI ) % M NEW_LINE return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 10 NEW_LINE B = 100 NEW_LINE C = 1000 NEW_LINE print ( findTripleSum ( A , B , C ) ) NEW_LINE DEDENT