Sum of i * countDigits ( i ) ^ countDigits ( i ) for all i in range [ L , R ] | Python 3 program to find the required sum ; Function to return the required sum ; Iterating for all the number of digits from 1 to 10 ; If the range is valid ; Sum of AP ; Computing the next minimum and maximum numbers by for the ( i + 1 ) - th digit ; Driver code
MOD = 1000000007 NEW_LINE def rangeSum ( l , r ) : NEW_LINE INDENT a = 1 NEW_LINE b = 9 NEW_LINE res = 0 NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT L = max ( l , a ) NEW_LINE R = min ( r , b ) NEW_LINE if ( L <= R ) : NEW_LINE INDENT sum = ( L + R ) * ( R - L + 1 ) // 2 NEW_LINE res += pow ( i , i ) * ( sum % MOD ) NEW_LINE res %= MOD NEW_LINE DEDENT a = a * 10 NEW_LINE b = b * 10 + 9 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 98 NEW_LINE r = 102 NEW_LINE print ( rangeSum ( l , r ) ) NEW_LINE DEDENT