Power Calculation

  • + 0 comments

    This is giving me Time Error Exceed, maybe due to the exponential operations

    Python
    
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    sys.setrecursionlimit(20000)
    
    #
    # Complete the 'solve' function below.
    #
    # The function is expected to return a STRING.
    # The function accepts following parameters:
    #  1. LONG_INTEGER k
    #  2. LONG_INTEGER n
    #
    
    def solve(k, n):
        # Write your code here
        if k == 1:
            return 1
        else:
            return k**n + solve(k-1, n)
            
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        t = int(input().strip())
    
        for t_itr in range(t):
            first_multiple_input = input().rstrip().split()
    
            k = int(first_multiple_input[0])
    
            n = int(first_multiple_input[1])
    
            result = solve(k, n)
            
            result = result % 100
            
            if result <10:
                strRes = "0"+str(result)
            else:
                strRes = str(result)
                
            fptr.write(strRes + '\n')
    
        fptr.close()