Sort by

recency

|

7 Discussions

|

  • + 1 comment

    !/bin/python3

    import math import os import random import re import sys

    #

    Complete the 'solve' function below.

    #

    The function is expected to return an INTEGER.

    The function accepts following parameters:

    1. INTEGER n

    2. INTEGER m

    #

    from math import gcd

    def solve(A): if A[0] == 1 or A[1] == 1: return 1 m = min(A) M = m + 1

    ###CALCULATION OF PRIMES<=M.###
    Primes = [0]*M
    i = 2
    while i*i<M:
        if not Primes[i]:
            j = i
            k = i*j
            while k<M:
                Primes[k] = 1
                j+=1
                k = i*j
        i+=1
    primes = []    
    for i in range(2,M):
        if Primes[i]==0:
            primes.append(i) 
    ################################
    
    res = 1
    MOD = 10**9+7
    f,s = A
    
    for p in primes:
        temp = p
        while temp<=m:
            res = (res*pow(p,(f//temp) * (s//temp),MOD))%MOD
            temp*=p
    
    return res%MOD        
    

    A = list(map(int, input().split())) print(solve(A))

  • + 1 comment

    Can any one expalin the solution for me ?

  • + 0 comments

    Python3 solution

    from math import gcd
    
    def primesTo(n):
        sieve = [True] * n
        for i in range(3, int(n ** 0.5) + 1, 2):
            if sieve[i]:
                sieve[i * i:: 2 * i] = [False] * ((n - i * i - 1) // (2 * i) + 1)
        return [2] + [i for i in range(3, n, 2) if sieve[i]]
    
    def solve(A):
        if A[0] == 1 or A[1] == 1:
            return 1
        m = min(A)
        primes = primesTo(m + 1)
        total = 1
        modo = 10 ** 9 + 7
        for p in primes:
            temp = p
            while temp <= m:
                total = (total * pow(p, (A[0] // temp) * (A[1] // temp), modo)) % modo
                temp *= p         
        return total % modo
    
    A = list(map(int, input().split()))
    print(solve(A))
    
  • + 0 comments

    Checked the solution after solving it. Turns out the author's solution is not the most optimal possible solution. A hint: the function is multiplicative, so try to think of the product as a product of prime powers. Then see if you can figure out what the powers of those primes are in the product.

  • + 0 comments

    I'd like to thank the Cuauhtémoc Moctezuma Brewery, makers of Dos Equis, for helping me reach the key insight in this problem. Stuck on the tip of my mind for hours.