All pastes #2107579 Raw Edit

Unnamed

public python v1 · immutable
#2107579 ·published 2012-01-29 22:00 UTC
rendered paste body
from math import sqrt, pow, ceil, floorcache = [1]def fact(n):    while n >= len(cache):        cache.append(len(cache)*cache[-1])    return cache[n]def c(m,n):    return fact(m)/(fact(n)*fact(m-n))def invpow(x,n):    """Finds the integer component of the n'th root of x,    an integer such that y ** n <= x < (y + 1) ** n.    """    high = 1    while high ** n < x:        high *= 2    low = high/2    while low < high:        mid = (low + high) // 2        if low < mid and mid**n < x:            low = mid        elif high > mid and mid**n > x:            high = mid        else:            return mid    return mid + 1def reverse_choose(n):    if n <= 1:        return 1    m = n    for k in range(2, 15):        lb = int(floor(invpow(fact(k)*n, k)))        ub = lb+k        for i in range(lb, ub+1):            if c(i,k) == n:                m = min(i,m)    return mdcache = {}def divisors(n):    if n in dcache:        return dcache[n]    d = set()    i = 1    while i < sqrt(n)+2:        if n % i == 0:            a,b = sorted((i, n//i))            d.add((a,b))        i += 1    dcache[n] = d    return ddef solution(S):    result = None    for a,b in divisors(S):        tmp = reverse_choose(a) + reverse_choose(b)        if result is None or tmp < result:            result = tmp    return result        def main():    T = int(raw_input())    for i in range(1, T+1):        S = int(raw_input())        print "Case #%d: %d" % (i, solution(S))if __name__=="__main__":    main()