Problem:
 Score : 300 points    Problem Statement To make it difficult to withdraw money, a certain bank allows its customers to withdraw only one of the following amounts in one operation:    1 yen (the currency of Japan)    6 yen, 6^2(=36) yen, 6^3(=216) yen, ...    9 yen, 9^2(=81) yen, 9^3(=729) yen, ...    At least how many operations are required to withdraw exactly N yen in total?  It is not allowed to re-deposit the money you withdrew.      Constraints  1 \leq N \leq 100000  N is an integer.         Input Input is given from Standard Input in the following format:  N       Output If at least x operations are required to withdraw exactly N yen in total, print x .        Sample Input 1 127      Sample Output 1 4  By withdrawing 1 yen, 9 yen, 36(=6^2) yen and 81(=9^2) yen, we can withdraw 127 yen in four operations.       Sample Input 2 3      Sample Output 2 3  By withdrawing 1 yen three times, we can withdraw 3 yen in three operations.       Sample Input 3 44852      Sample Output 3 16

Solution (Python):
import sys

def f(n):
  X6 = []
  X9 = []
  for i in range(1, 10):
    if n >= 6**i:
      X6.append(6**i)
    if n >= 9**i:
      X9.append(9**i)
  return X6, X9


def main():
  input = sys.stdin.readline
  N = int(input())
  dp = [0 for _ in range(N+1)]
  for i in range(1, N+1):
    can = []
    X6, X9 = f(i)
    X = [1] + X6 + X9
    for x in X:
      can.append(dp[i-x] + 1)
    dp[i] = min(can)
  print(dp[N])

if __name__ == '__main__':
  main()

Solution (Python):

import copy
import numpy as np
import random
import queue

n = int(input())

withdraws = [1]
price = 1
while True:
    price *= 6
    if price <= 10**5 and price <= n:
        withdraws.append(price)
    else:
        break

price = 1
while True:
    price *= 9
    if price <= 10**5 and price <= n:
        withdraws.append(price)
    else:
        break


withdraws.sort()

money = 0
count = 0



'''
que = queue.Queue()

que.put((0,0))
memory = []
while not(que.empty()):
    money,count = que.get()
    print(money,count)
    if money > n:
        pass
    else:
        for withdraw in withdraws:
            if money+withdraw == n:
                print(count+1)
                exit()

            elif money+withdraw in memory:
                pass
            else:
                memory.append(money+withdraw)
                next = (money+withdraw,count+1)
                que.put(next)
'''
'''


ans = 10**10
if n < 6:
    ans = n
for i in range(n+1):
    t = i
    count = 0
    while t>0:
        count += t%6
        t = t//6
    t = n - i
    while t>0:
        count += t%9
        t = t//9
    
    ans =min(ans,count)

print(ans)

'''

max_num = 10**5
dp = [99999999]*(n+1)
dp[0] = 0
for i in range(1,n+1):
    
    dp[i] = dp[i-1]+1
    k = 6
    while i>=k:
        dp[i] = min(dp[i],dp[i-k]+1)
        k *= 6
    k = 9
    while i>=k:
        dp[i] = min(dp[i],dp[i-k]+1)
        k *= 9



print(dp[n])

Solution (Python):

import copy
import numpy as np
import random
import queue

n = int(input())

withdraws = [1]
price = 1
while True:
    price *= 6
    if price <= 10**5 and price <= n:
        withdraws.append(price)
    else:
        break

price = 1
while True:
    price *= 9
    if price <= 10**5 and price <= n:
        withdraws.append(price)
    else:
        break


withdraws.sort(reverse = True)

money = 0
count = 0



'''
que = queue.Queue()

que.put((0,0))
memory = []
while not(que.empty()):
    money,count = que.get()
    print(money,count)
    if money > n:
        pass
    else:
        for withdraw in withdraws:
            if money+withdraw == n:
                print(count+1)
                exit()

            elif money+withdraw in memory:
                pass
            else:
                memory.append(money+withdraw)
                next = (money+withdraw,count+1)
                que.put(next)
'''



ans = 10**10
if n < 6:
    ans = n
for i in range(n+1):
    t = i
    count = 0
    while t>0:
        count += t%6
        t = t//6
    t = n - i
    while t>0:
        count += t%9
        t = t//9
    
    ans =min(ans,count)

print(ans)

Solution (Python):
s=lambda a,b:a if a<b else s(a//b,b)+a%b;n=int(input());print(min(s(i,6)+s(n-i,9)for i in range(n+1)))

Solution (Python):
N = int(input())

def f(x):
    ans = 0
    while x>0:
        ans += x%6
        x //= 6
    return ans

def g(x):
    ans = 0
    while x>0:
        ans += x%9
        x //= 9
    return ans

l = []

for x in range(N+1):
    l.append(f(x)+g(N-x))

print(min(l))

Solution (Python):
n = int(input())

def search(n,num):
    i = 1
    while True:
        if n < num**i:
            return i-1
        i += 1
        
ans = n
for i1 in range(0,6):
    for i2 in range(0,6):
        for i3 in range(0,6):
            for i4 in range(0,6):
                for i5 in range(0,6):
                    for i6 in range(0,6):
                        res = n
                        res -= (6**1)*i1+(6**2)*i2+(6**3)*i3+(6**4)*i4+(6**5)*i5+(6**6)*i6
                        if res >= 0:
                            count = i1+i2+i3+i4+i5+i6
                            while res > 6:
                                i = search(res,9)
                                cnt = int(res / (9**i))
                                res -= (9**i) * cnt
                                count += cnt
                            count += res
                            if ans > count:
                                ans = count
print(ans)

Solution (Python):
n = int(input())

ans = 10 ** 5
for i in range(n // 9 + 1):
    m = n - 9 * i
    j, k = divmod(m, 6)
    cnt = k

    tmp = 0
    while 6 ** (tmp + 1) <= j: tmp += 1
    while tmp > 0 and j:
        jj, j = divmod(j, 6 ** tmp)
        cnt += jj
        tmp -= 1
    cnt += j

    tmp = 0
    while 9 ** (tmp + 1) <= i: tmp += 1
    while tmp > 0 and i:
        ii, i = divmod(i, 9 ** tmp)
        cnt += ii
        tmp -= 1
    cnt += i

    ans = min(ans, cnt)

print(ans)

Solution (Python):
def counting(num, power, base):
    num2 = 0
    while num >= base:
        if num >= base ** (power - 1):
            num -= base ** (power - 1)
            num2 += 1
        else:
            power -= 1
    return num + num2

def main():
    n = int(input())
    ans = float("inf")
    for n_nine in range(0, n// 9 + 1):
        s = n - 9 * n_nine
        n_six = s // 6
        n_1 = s - 6 * n_six
        pow_nine = 5
        pow_six = 6
        n_9 = counting(n_nine, pow_nine, 9)
        n_6 = counting(n_six, pow_six, 6)
        ans = min(ans, n_1 + n_6 + n_9)

    print(ans)

main()

Solution (Python):
n = int(input())
ans = float("inf")
for n_nine in range(0, n//9 + 1):
    s = n - 9 * n_nine
    n_six = s // 6
    n_one = s - 6 * n_six
    n_nine2 = 0
    n_six2 = 0
    pow_nine = 5
    pow_six = 6
    
    while n_nine >= 9:
        if n_nine >= 9 ** (pow_nine - 1):
            n_nine -= 9 ** (pow_nine - 1)
            n_nine2 += 1
        else:
          pow_nine -= 1

    while n_six >= 6:
        if n_six >= 6 ** (pow_six - 1):
            n_six -= 6 ** (pow_six - 1)
            n_six2 += 1
        else:
          pow_six -= 1          
          
    ans = min(ans, n_nine2 + n_nine + n_six2 + n_six + n_one)
          
print(ans)

Solution (Python):
def solve(n):
    dp = [100001] * (n + 1)
    dp[0] = 0
    for i in range(n):
        for j in range(10):
            if i + 6 ** j > n:
                break
            dp[i + 6 ** j] = min(dp[i + 6 ** j], dp[i] + 1)
        for j in range(10):
            if i + 9 ** j > n:
                break
            dp[i + 9 ** j] = min(dp[i + 9 ** j], dp[i] + 1)
    return dp[n]

N = int(input())
print(solve(N))

Solution (Python):


def main():
    max_val = 10**5 + 1
    dp = [i for i in range(max_val)]

    for i in range(max_val):
        p = 1
        while p < max_val:
            dp[i] = min(dp[i], dp[i - p] + 1)
            p *= 6
        p = 1
        while p < max_val:
            dp[i] = min(dp[i], dp[i - p] + 1)
            p *= 9

    N = int(input())
    print(dp[N])


if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
DP=[i for i in range(100001)]
for i in range(100001):
    p=1
    while p<=i:
        DP[i]=min(DP[i],DP[i-p]+1)
        p*=6
    p=1
    while p<=i:
        DP[i]=min(DP[i],DP[i-p]+1)
        p*=9
print(DP[n])

Solution (Python):
n=int(input())

res=n
for i in range(n+1):
    count=0
    j=n-i
    while i>0:
        count+=i%6
        i//=6
    while j>0:
        count+=j%9
        j//=9
    if res>count:
        res=count

print(int(res))

Solution (Python):
N = int(input())

dp = [10**9] * (N+1)
dp[0], dp[1] = 0, 1
i = 6
while i<=N:
    dp[i] = 1
    i *= 6
i = 9
while i<=N:
    dp[i] = 1
    i *= 9

for i in range(1,N):
    dp[i+1] = min(dp[i+1], dp[i]+1)
    j = 6
    while i+j <= N:
        dp[i+j] = min(dp[i+j], dp[i]+1)
        j *= 6
    j = 9
    while i+j <= N:
        dp[i+j] = min(dp[i+j], dp[i]+1)
        j *= 9

print(dp[N])

Solution (Python):

N = int(input())

dp = [10**9] * (N+1)
dp[1] = 1


i = 6
while i <= N:
    dp[i] = 1
    i *= 6
i = 9
while i <= N:
    dp[i] = 1
    i *= 9

for i in range(1, N):
    if dp[i] != 10**9:
        dp[i+1] = min(dp[i+1], dp[i]+1)
        j = 6
        while i+j <= N:
            dp[i+j] = min(dp[i+j], dp[i]+1)
            j *= 6
        j = 9
        while i+j <= N:
            dp[i+j] = min(dp[i+j], dp[i]+1)
            j *= 9
print(dp[N])

Solution (Python):
n=int(input())
dp=[i for i in range(100001)]
for i in range(100001):
    p=1
    while p<=i:
        dp[i]=min(dp[i],dp[i-p]+1)
        p*=6
    p=1
    while p<=i:
        dp[i]=min(dp[i],dp[i-p]+1)
        p*=9
else:
    print(dp[n])

Solution (Python):
n = int(input())

max_range = 10 ** 5
dp = [int(i) for i in range(max_range + 1)]

for i in [6,9]:
    check = i
    while check < max_range:
        for j in range(max_range + 1):
            if j - check >= 0:
                dp[j] = min(dp[j],dp[j-check] + 1)

        check =  check * i
        
print(dp[n])

Solution (Python):
import itertools
import numpy as np
import sys
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N = I()

ans = 10e10
for i in range(N+1):
    j = N-i
    cnt = 0
    while i > 0:
        cnt += i%6
        i //= 6
    while j > 0:
        cnt += j%9
        j //= 9
    ans = min(ans,cnt)

print(ans)

Solution (Python):
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N] = [long(input_raw[0])]
    coin_list = [1]
    for i in range(1, N):
        if N >= 6 ** i:
            coin_list.append(6 ** i)
        else:
            break

    for i in range(1, N):
        if N >= 9 ** i:
            coin_list.append(9 ** i)
        else:
            break

    dp = [[0 for i in range(N + 1)] for i in  range(len(coin_list) + 1)]
    for j in range(N + 1):
        dp[0][j] = j
    min_dpN = N
    for i in range(1, len(coin_list)):
        for j in range(N + 1):
            if j - coin_list[i] >= 0:
                dp[i][j] = min(dp[i - 1][j], dp[i][j - coin_list[i]] + 1)
            else:
                dp[i][j] = dp[i - 1][j]
        min_dpN = min(dp[i][N], min_dpN)
    print min_dpN

if __name__ == '__main__':
    main()