Sort by

recency

|

12 Discussions

|

  • + 0 comments

    My Python Loopless Solution

    With Derivation of Answer

    def solve(a, n, m):
    	digits = len(str(a))
    	divisor = 10**digits-1
    	m *= divisor
    	return (a % m)*(pow(10, digits*n, m)-1) % m // divisor
    

    Explanation

  • + 0 comments

    Python3 one line solution:

    def solve(a, n, m):
        return(((((pow(10,n*(len(str(a))),(10**(len(str(a))) - 1)*m)-1)//(10**(len(str(a))) - 1))%m)*(a%m)%m))
    
  • + 0 comments
    def solve(a, n, m):
        a = a % m
        pow10  = 10 ** len(str(a)) % m
        answer = 0
    
        while n:
            if n & 1:  #ie n is odd number
                answer = ((answer * pow10) % m + a) % m 
    
            a  = ((a * pow10) % m + a) % m
            pow10 = (pow10 * pow10) % m 
            n >>= 1 
    
        return answer
    

    https://www.rookieslab.com/posts/fast-power-algorithm-exponentiation-by-squaring-cpp-python-implementation

  • + 0 comments

    OK, THE BIG NUMBER really upsets me. For Python3,

    #!/bin/python3
    
    import sys
    
    def power(a, n, m):
        if n == 0:
            return 1
        elif n == 1:
            return a % m
        elif not n % 2:
            return ((power(a, n // 2, m) % m)**2) % m
        else:
            return ((a % m) * (((power(a, (n - 1)// 2, m)) % m)**2) % m) % m
    
    def cal(b, n, m):
        if n == 0:
            return 0
        elif n == 1:
            return b % m
        elif not n % 2:
            return (((1 + power(b, n // 2, m)) % m) * (cal(b, n // 2, m) % m)) % m
        else:
            return (((1 + power(b, (n + 1) // 2, m)) % m) * (cal(b, ((n - 1) // 2), m) % m) + power(b, (n + 1) // 2, m) % m) % m
    
    def solve(a, n, m):
        l = len(str(a))
        x = a % m
        q = 10**l % m
        return (x * (1 + cal(q, n - 1, m))) % m
    
    for _ in range(int(input())):
        a,n,m = [int(temp) for temp in input().split()]
        print(solve(a, n, m))
    
  • + 0 comments

    Can someone please help me with my solution. I tried to do what was said in the tutorial but not getting the correct answer. Its a long time i am stuck in this question Please someone help. My solution link :

    https://ideone.com/pMo16z My another approach using pointers https://ideone.com/h6YiDy