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



Sample input:
127


Sample output:
4

Submission status: Wrong Answer

Solution code:

N = int(input())

v = [1]

for i in range(1,7):
    a = pow(6,i)
    if a > N:
        break
    v.append(a)
    v.append(pow(9,i))

res = 0

v.sort(reverse=True)
while True:
    for j, i in enumerate(v):
        if i <= N:
            N = N - i
            res += 1
            v = v[j:]
            break
    if N == 0:
        print(res)
        break

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0

def cal(N,num):
    if N >= num:
        for i in range(1,999):
            if N < num ** i:
                return num ** (i-1)
                break

    return -1


while N >= 6:
    res_6 = cal(N,6)
    res_9 = cal(N,9)

    if N < 18:
        break
    elif res_9 != -1 and N >= res_9 and res_9 > res_6:
        N -= res_9
        count += 1
    elif res_6 != -1 and N >= res_6:
        N -= res_6
        count += 1
tmp_N = N
tmp_count = 0
while tmp_N >= 0:
    if tmp_N >= 9:
        tmp_N -= 9
        tmp_count+=1
    elif tmp_N >= 6:
        tmp_N -= 6
        tmp_count += 1
    else:
        tmp_count += tmp_N
        break

if int(N/6)+N%6 < tmp_count:
    count += int(N/6) + N % 6
else:
    count += tmp_count

print(count)

Submission status: Wrong Answer

Solution code:
N = int(input())

ans = 100000
temp = 0

for i in range(6):
    N_i = N - i
    temp = 0
    for cand in reversed(sorted([9**i for i in range(1, 6)] + [6**i for i in range(1,7)])):
        if N_i >= cand:
            i = 1
            while N_i >= i * cand:
                 i += 1
            N_i = N_i - (i - 1) * cand
            temp += i - 1

        if N_i == 0:
            break

    temp += i
    if temp < ans:
        ans = temp

print(ans)

Submission status: Wrong Answer

Solution code:
import math
import sys

N = int(sys.stdin.readline())

queue = [(N, 0)]
check = set()
MAX_N = 100000
min_cnt = MAX_N

def add_queue(X):
    if X not in check:
        check.add(X)
        queue.append(X)

while queue:
    N, cnt = queue.pop()
    if N >= 9:
        v = int(math.pow(9, int(math.log(N, 9))))
        if N - v >= 6 and cnt < min_cnt:
            add_queue((N - v, cnt + 1))
        else:
            min_cnt = min(min_cnt, cnt + (N - v) + 1)
    if N >= 6:
        v = int(math.pow(6, int(math.log(N, 6))))
        if N - v >= 6 and cnt < min_cnt:
            add_queue((N - v, cnt + 1))
        else:
            min_cnt = min(min_cnt, cnt + (N - v) + 1)

if min_cnt == MAX_N:
    print(N)
else:
    print(min_cnt)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())

p = [[59049, 1], [46656, 1], [7776, 1], [6561, 1], [1296, 1], [864, 4], [
    729, 1], [216, 1], [81, 1], [36, 1],[18,2], [12, 2], [9, 1], [6, 1]]

count = 0

for i in range(14):
    count += int(n / p[i][0]) * p[i][1]
    n = n % p[i][0]

print(count + n)

Submission status: Wrong Answer

Solution code:
n = int(input())
print(n)

Submission status: Wrong Answer

Solution code:
import numpy as np

N = int(input())

pow6 = [1]
pow9 = [1]

i=0
while pow6[i]*6 <= N:
    pow6.append(6*pow6[i])
    i += 1

i=0
while pow9[i]*9 <= N:
    pow9.append(9*pow9[i])
    i += 1

list_69 = pow6 + pow9
list_69 = sorted(list_69)


ans = 0
while N >= 6:

    list_69 = [j for j in list_69 if j <= N]

    N -= list_69[-1]
    ans = ans + 1


ans = ans + N
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
N = int(input())
num = []
ans = 0
for i in range(1,6):
  num.append(6**i)
  num.append(9**i)
num.append(6**6)
num.sort()
num.reverse()
if N < 6:
  print(N)
else:
  for i in num:
    if i == N:
      print(1)
      sys.exit()
  for i in range(len(num)):
    while num[i] < N:
      N -= num[i]
      ans += 1
    if N < 6 and N != 0:
      ans += N
      break
  print(ans)

Submission status: Wrong Answer

Solution code:
def power_of_six(n):
    now = 1
    power = 0
    while now <= n:
        now = now * 6
        power = power + 1
    return power-1

def power_of_nine(n):
    now = 1
    power = 0
    while now <= n:
        now = now * 9
        power = power + 1
    return power-1

def main():
    N = input()
    x = 0
    powsix = 0
    pownine = 0
    while N > 0:
        if N < 18:
            if N < 6:
                x = x + N
                break
            else:
                N = N - 6
                x = x + 1
        else:
            powsix = power_of_six(N)
            pownine = power_of_nine(N)
            if 6**powsix > 9**pownine:
                N = N - 6**powsix
                x = x + 1
            else:
                N = N - 9**pownine
                x = x + 1
    print(x)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def getResult(tmpList, zan):
    r = 0
    for i in tmpList:
        tmp = zan // i
        if tmp != 0:
            r = r + tmp
            zan = zan % i
    return r


n = int(input())

limit = n
inputList = [1]

nine = 9
while nine <= limit:
    inputList.append(nine)
    nine = nine * 9

six = 6
while six <= limit:
    inputList.append(six)
    six = six * 6

inputList.sort(reverse=True)

copyList = inputList.copy()
result = n
while len(copyList) >= 1:
    tmpResult = getResult(copyList, n)
    copyList.pop(0)
    if result > tmpResult:
        result = tmpResult

print(result)

Submission status: Wrong Answer

Solution code:
import math
import sys

N = int(sys.stdin.readline())

queue = [(N, 0)]
check = set()
MAX_N = 100000
min_cnt = MAX_N

def add_queue(X):
    if X not in check:
        check.add(X)
        queue.append(X)

while queue:
    N, cnt = queue.pop()
    if N >= 9:
        for i in range(1, int(math.log(N, 9)) + 1):
            v = int(math.pow(9, i))
            if N - v >= 6 and cnt < min_cnt:
                add_queue((N - v, cnt + 1))
            elif cnt + (N - v) + 1 < min_cnt:
                min_cnt = cnt + int(N - v) + 1
    if N >= 6:
        for i in range(1, int(math.log(N, 6)) + 1):
            v = int(math.pow(6, i))
            if N - v >= 6 and cnt < min_cnt:
                add_queue((N - v, cnt + 1))
            elif cnt + (N - v) + 1 < min_cnt:
                min_cnt = cnt + int(N - v) + 1

if min_cnt == MAX_N:
    print(N)
else:
    print(min_cnt)

Submission status: Wrong Answer

Solution code:
n = int(raw_input())
m = [59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1]

times = 0

for i in m:
    if n>=i:
        times += n/i
        n = n%i

print times

Submission status: Wrong Answer

Solution code:
n = int(input())
money_list = (59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1)
ans = 0
for i in money_list:
  while n >= i:
    if (46656*2) <= n:
      n -= 46656
      ans += 1
    elif (6561*2) <= n < (7776+6561):
      n -= 6561
      ans += 1
    elif (729*2) <= n < (1296+729):
      n -= 729
      ans += 1
    elif (81*3) <= n <= (81*3+2) or (81*3+6) <= n < (216+36): #249: #(245):
      n -= 81
      ans += 1
    elif 12 <= n < 15:
      n -= 6
      ans += 1
    else:
      n -= i
      ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3

n = int(input())

bs = set()
for i in range(6):
    bs.add(6 ** (i + 1))

for i in range(5):
    bs.add(9 ** (i + 1))

bss = sorted(bs, reverse=True)

cnt = 0
for i in range(len(bss)):
    while n > bss[i]:
        if bss[i] == 9 and 11 < n < 18:
            break
        n -= bss[i]
        cnt += 1

print(cnt + n)

Submission status: Wrong Answer

Solution code:
N = int(input())

c = 0
for i in reversed(range(2, 10)):
    p = pow(9, i)
    while p < N:
        N = N - p
        c = c + 1
        # print(c, p, N)
    p = pow(6, i)
    while p < N:
        N = N - p
        c = c + 1
        # print(c, p, N)

# まずは6の倍数のカタチにする
a, b = divmod(N, 6)

for _ in range(a):
    if b > 3:
        b = b - 3

c = c + a + b

print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
ichi = 1
roku = [6]
cnt = 2
while roku[-1] < 100000:
    roku.append(6**cnt)
    cnt += 1
kyu = [9]
cnt = 2
while kyu[-1] < 100000:
    kyu.append(9**cnt)
    cnt += 1
cnt = 0
kr = kyu + roku
kr.sort()
kr.reverse()
kr.append(1)
for i in kr:
    if i <= n:
        tmp = n // i
        n -= tmp * i
        cnt += tmp

print(cnt)

Submission status: Wrong Answer

Solution code:
# input
N = int(input())

def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)

def min_69(X):
    count = 0
    while X > 26:
        X_6 = Base_10_to_n(X, 6)
        X_9 = Base_10_to_n(X, 9)
        if 6 ** (len(X_6) - 1) >= 9 ** (len(X_9) - 1):
            count += 1
            X = X - 6 ** (len(X_6) - 1)
        else:
            count += 1
            X = X - 9 ** (len(X_9) - 1)
    if X == 1 or X == 6 or X == 9:
        count += 1
    if X == 2 or X == 7 or X == 10 or X == 12 or X == 15 or X == 18:
        count += 2
    if X == 3 or X == 8 or X == 11 or X == 13 or X == 16 or X == 19 or X == 21 or X == 24:
        count += 3
    if X == 4 or X == 14 or X == 17 or X == 20 or X == 22 or X == 25:
        count += 4
    if X == 5 or X == 23 or X == 26:
        count += 5
    return count

ans = min_69(N)

print(ans)

Submission status: Wrong Answer

Solution code:
def calc(list,yen,count):
    ##print('yenは'+str(yen))
    ##print('countは'+str(count))

    for i in list:
        if yen > i:
            return calc(list,yen%i,count+1)
            break
    else:
        return count+yen



yen = int(input())
from time import sleep
counta = 1
list = []
##list.append(1)

x=0
while x < 100000:
    x = 6**counta
    list.append(x)
    counta = counta + 1

counta = 1
x=0

while x < 100000:
    x = 9**counta
    list.append(x)
    counta = counta + 1

list.sort()
list.reverse()
print(calc(list,yen,0))

Submission status: Wrong Answer

Solution code:
import math
import sys

N = int(sys.stdin.readline())

queue = [(N, 0)]
check = set()
MAX_CNT = 9999999
min_cnt = MAX_CNT

def add_queue(X):
    if X not in check:
        check.add(X)
        queue.append(X)

while queue:
    N, cnt = queue.pop()
    if N >= 9:
        v = int(math.pow(9, int(math.log(N, 9))))
        if N - v >= 6 and cnt < min_cnt:
            add_queue((N - v, cnt + 1))
        elif cnt + (N - v) + 1 < min_cnt:
            min_cnt = cnt + int(N - v) + 1
    if N >= 6:
        v = int(math.pow(6, int(math.log(N, 6))))
        if N - v >= 6 and cnt < min_cnt:
            add_queue((N - v, cnt + 1))
        elif cnt + (N - v) + 1 < min_cnt:
            min_cnt = cnt + int(N - v) + 1

if min_cnt == MAX_CNT:
    print(N)
else:
    print(min_cnt)

Submission status: Wrong Answer

Solution code:
money = []
N=127
n=9
m=6
while n < 100000:
    money.append (n)
    n = n*9

while m < 100000:
    money.append (m)
    m = m*6

money.sort()
money.reverse()

times = 0

for i in money:
    times += int(N/i)
    N -= int(N/i) * i
    if N<36:
        break

possibility = []
for j in range(0,int(N/9)+1):
    M = N-9*j
    possibility.append(j + int(M/6) + M%6)

print (times + min(possibility))

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
numbers = sorted([9**5, 9**4, 9**3, 9**2, 9, 6**6, 6**5, 6**4, 6**3, 6**2, 6], reverse=True)

def search(n):
    for i in numbers:
        if i <= n:
            return i

def search_max(n):
    mincount = 1000
    inp = n
    for i in range(3):
        n = inp
        n -= 9*i
        mincount = min((i + n // 6 + n % 6), mincount)
    print(mincount)
    return mincount


for i in range(100):
    if 1 <= N <= 5:
        count += N
        break
    elif N == 0:
        break
    elif 5 < N < 35:
        count += search_max(N)
        break
    else:
        count += N // search(N)
        N %= search(N)

print(count)

Submission status: Wrong Answer

Solution code:
def main():
    #0. input
    
    
    #1. make list
    money_list = [1]
    for i in range(1,7):
        money_list.append(6**i)
        
    for i in range(1,6):
        money_list.append(9**i)
        
    money_list.sort(reverse=True)

    
    #2. count
    count = 0
    count_temp = 0
    
    rest = int(input())
    rest_org = rest
    
    for money in money_list:
        if rest < money:
            continue
        else:
            count_temp = rest//money
            count += count_temp
            rest -= count_temp*money
            
            if money==1 and count_temp>2 and rest_org>10:
                count -= 2

        
    print(count)
    
    
if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

c = 0
for i in reversed(range(2, 10)):
    p = pow(9, i)
    while p < N:
        N = N - p
        c = c + 1
        # print(c, p, N)
    p = pow(6, i)
    while p < N:
        N = N - p
        c = c + 1
        # print(c, p, N)

# まずは6の倍数のカタチにする
a, b = divmod(N, 6)
b = b % 3

c = c + a + b

print(c)

Submission status: Wrong Answer

Solution code:
import sys

N = int(input())
money = [1]
for i in range(1,N):
    if 6**i <= N:
        money.append(6**i)
    if 9**i <= N:
        money.append(9**i)
    else:
        break


def dfs(rest,cnt):
    if rest == 0:
        print(cnt)
        sys.exit()
    for i in range(1,rest):
        if len(money) < i: break
        if rest-money[-i] >= 0:
            tmp = cnt+1
            dfs(rest-money[-i],tmp)
        

dfs(N, 0)

Submission status: Wrong Answer

Solution code:
import math

N = int(input())
ans = 0
while 1:
	if N >= 36:
		N -= max(pow(6,math.floor(math.log(N,6))),pow(9,math.floor(math.log(N,9))))
		ans += 1
	else:
		tmp = 114514
		for i in range(1+N//9):
			tmp = min(tmp,i+(N-9*i)//6+(N-9*i)%6)
		ans += tmp
		N = 0
	if N == 0:
		break
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n = int(input())

p = [[59049, 1], [46656, 1], [7776, 1], [6561, 1], [1296, 1], [864, 4], [
    729, 1], [216, 1], [108, 3], [81, 1], [36, 1], [12, 2], [9, 1], [6, 1]]

count = 0

for i in range(14):
    count += int(n / p[i][0]) * p[i][1]
    n = n % p[i][0]

print(count + n)

Submission status: Wrong Answer

Solution code:
import math
import sys

N = int(sys.stdin.readline())

queue = [(N, 0)]
MAX_CNT = 9999999
min_cnt = MAX_CNT

while queue:
    N, cnt = queue.pop()
    if N >= 9:
        v = int(math.pow(9, int(math.log(N, 9))))
        if N - v >= 6 and cnt < min_cnt:
            queue.append((N - v, cnt + 1))
        elif cnt + (N - v) + 1 < min_cnt:
            min_cnt = cnt + int(N - v) + 1
    if N >= 6:
        v = int(math.pow(6, int(math.log(N, 6))))
        if N - v >= 6 and cnt < min_cnt:
            queue.append((N - v, cnt + 1))
        elif cnt + (N - v) + 1 < min_cnt:
            min_cnt = cnt + int(N - v) + 1

if min_cnt == MAX_CNT:
    print(N)
else:
    print(min_cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
count=0
while n>=0:
  if n<=5:
    count=count+n
    print(count)
    break
  elif n<=8:
    count=count+n-5
    print(count)
    break
  else:
    i=1
    next1=0
    while 6**i<=n:
      next1=6**i
      i+=1
    j=1
    next2=0
    while 9**j<=n:
      next2=9**j
      j+=1
    if next1>next2:
      n=n-next1
    else:
      n=n-next2
    count+=1

Submission status: Wrong Answer

Solution code:
N = int(input())
count = 0
itr = 1

while(N >= 9 or N >= 6):
    itr = 1
    flag = 0
    itr2 = 0
    while(N >= 9**itr or N >= 6**itr):
        itr += 1
        if N < 9**itr and flag == 0:
            itr2 = itr-1
            flag = 1
    if flag == 1:
        if 9**itr2 > 6**(itr-1):
            N = N - 9**itr2
            print(N, 9**itr2, 9)
        else:
            N = N - 6**(itr-1)
            print(N, 6**(itr-1), 6)
    else:
        if 9**(itr-1) > 6**(itr-1):
            N = N - 9**(itr-1)
            print(N, 9**(itr-1), 9)
        else:
            N = N - 6**(itr-1)
            print(N, 6**(itr-1), 6)
    count += 1
    if N == 0:
        break
if N > 0:
    count += N
print(count)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import os
import sys
import math
import numpy as np


def func(n, c):
    global ans


    if n == 0:
        if c < ans:
            ans = c
        return

    p = int(math.log(n, 6))
    q = int(math.log(n, 9))
    # print(n, p, q, c, ans)

    if p == 0 and q == 0:
        if c + n < ans:
            ans = c + n
        # return n
    else:
        # varp = int(n/(6**p))
        # print(n, n-(varp*(6**p)))
        # func(n-(varp*(6**p)), c + varp)
        if c+1 <= ans:
            if p > 0:
                func(n-(6**p), c + 1)
            if q > 0:
                func(n-(9**q), c + 1)

        # varq = int(n/(9**q))
        # print(n, n-(varq*(9**q)))
        # func(n-(varq*(9**q)), c + varq)

        # print(varp, varq)


def main():
    n = int(input())
    global ans
    ans = 10**10
    c = 0

    func(n, c)
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N = int(input())

count = 0
while N>5:
    
    hiku9 = 1
    while hiku9 <= N:
        hiku9 = hiku9 * 9
    
    hiku6 = 1
    while hiku6 <= N:
        hiku6 = hiku6 * 6
    
    N = N - max(hiku6/6,hiku9/9)
    count  = count + 1
    print(N)


print(count + N)

Submission status: Wrong Answer

Solution code:
#入力
N = int(input())
#m:残りの金額、s1:べき乗、s2:9,6^s1,x:回数
m = N
s1 = [0,0]
s2 = [0,0,1]
x = 0

#残りの金額が0になるまで続ける
while m > 0:
    #9,6でmを上回らない最大のべき乗を求める
    #9について
    while 9**(s1[0]+1)<=m:
        s1[0] = s1[0]+1
    #6について
    while 6**(s1[1]+1)<=m:
        s1[1] = s1[1]+1
    s2 = [9**s1[0], 6**s1[1], 1]
    s2.sort()
    #最も大きい金額を引き出して回数を+1
    m = m - s2[2]
    x = x + 1
    #初期化
    s1 = [0,0]
    s2 = [0,0,1]
#xを出力
print(x)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import os
import sys
import math
import numpy as np


def func(n, c):
    global ans


    if n == 0:
        if c < ans:
            ans = c
        return

    p = int(math.log(n, 6))
    q = int(math.log(n, 9))
    # print(n, p, q, c, ans)

    if p == 0 and q == 0:
        if c + n < ans:
            ans = c + n
        # return n
    else:
        # varp = int(n/(6**p))
        # print(n, n-(varp*(6**p)))
        # func(n-(varp*(6**p)), c + varp)
        if c+1 <= ans:
            func(n-(6**p), c + 1)
            func(n-(9**q), c + 1)

        # varq = int(n/(9**q))
        # print(n, n-(varq*(9**q)))
        # func(n-(varq*(9**q)), c + varq)

        # print(varp, varq)


def main():
    n = int(input())
    global ans
    ans = 10**10
    c = 0

    func(n, c)
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
ans = 0
money = [1]
for i in range(1,7):
    money.append(6**i)
    money.append(9**i)
money.sort()
money.reverse()
for i in money:
    if n >= i:
        a = n//i
        #print(n,i,a)
        ans += a
        n -= a*i
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import stdin


def main():
    N = int(stdin.readline().rstrip())
    a = [y for y in [6 ** x for x in range(1, 10)] if y <= 100000]
    b = [y for y in [9 ** x for x in range(1, 10)] if y <= 100000]
    c = a + b

    count = 0

    if N < 18:
        if N == 17:
            print(count + 4)
        elif N == 16:
            print(count + 3)
        elif N == 15:
            print(count + 2)
        elif N == 14:
            print(count + 4)
        elif N == 13:
            print(count + 3)
        elif N == 12:
            print(count + 2)
        elif N == 11:
            print(count + 3)
        elif N == 10:
            print(count + 2)
        elif N == 9:
            print(count + 1)
        elif N == 8:
            print(count + 3)
        elif N == 7:
            print(count + 2)
        elif N == 6:
            print(count + 1)
        else:
            print(count + N)
        return

    while True:
        tmp = max([x for x in c if x <= N])
        N -= tmp
        count += 1
        if N < 18:
            if N == 17:
                print(count + 4)
            elif N == 16:
                print(count + 3)
            elif N == 15:
                print(count + 2)
            elif N == 14:
                print(count + 4)
            elif N == 13:
                print(count + 3)
            elif N == 12:
                print(count + 2)
            elif N == 11:
                print(count + 3)
            elif N == 10:
                print(count + 2)
            elif N == 9:
                print(count + 1)
            elif N == 8:
                print(count + 3)
            elif N == 7:
                print(count + 2)
            elif N == 6:
                print(count + 1)
            else:
                print(count + N)
            return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys 
n=int(input())
ans=0
k=n%18
if n<6:
  print(n)
  sys.exit()
if 0<k<6:
  ans+=k
  n-=k
if 6<k<9:
  ans+=k-6
  n-=k-6
if 9<k<12:
  ans+=k-9
  n-=k-9
if 12<k<18:
  ans+=k-12
  n-=k-12
while True:
  i=0
  flg1=False
  flg2=False
  if n==0:
    break
  if n==1 or n==6 or n==9:
    ans+=1
    break
  if n==2 or n==7 or n==10 or n==12 or n==15 or n==18:
    ans+=2
    break
  if n==3 or n==8 or n==11 or n==13 or n==16 or n==19:
    ans+=3
    break
  if n==4 or n==14 or n==17 or n==20:
    ans+=4
    break
  if n==5:
    ans+=5
    break
  while True:
    if 6**i>n and (not flg1):
      s=i-1
      flg1=True
    if 9**i>n and (not flg2):
      t=i-1
      flg2=True
    if flg1 and flg2:
      break
    i+=1
  n-=max(6**s,9**t)
  ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
# judge = True
ans = 0
temp = 9
temp_list = [1,2,3,4,5]
while 100000>temp:
    temp_list.append(temp)
    temp *=9 
temp = 6
while 100000>temp:
    temp_list.append(temp)
    temp *=6 
temp_list=sorted(temp_list)
# print(temp_list)
for i in range(1,len(temp_list)+1):
    judge = True
    while judge:
        if N - temp_list[-i] > 9 and (temp_list[-i] >5):
            # print(N)
            # print(temp_list[-i])
            N -= temp_list[-i]
            ans+=1
        else:
            judge = False
# print(N)
# print(N//9)
# print(N//6)
# print(N)
if N%9 < N%6:
    ans += N//9
    N = N%9
    # N -= 9*N//9
else:
    ans += N//6
    N = N%6
    # N -= 6*N//6
# print(N)
# print(ans)
print(N+ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-

import os
import sys
import math
import numpy as np


def func(n, c):
    global ans


    if n == 0:
        if c < ans:
            ans = c
        return

    p = int(math.log(n, 6))
    q = int(math.log(n, 9))
    # print(n, p, q, c, ans)

    if p == 0 and q == 0:
        if c + n < ans:
            ans = c + n
        # return n
    else:
        # varp = int(n/(6**p))
        # print(n, n-(varp*(6**p)))
        # func(n-(varp*(6**p)), c + varp)
        if c+1 < ans:
            func(n-(6**p), c + 1)
            func(n-(9**q), c + 1)

        # varq = int(n/(9**q))
        # print(n, n-(varq*(9**q)))
        # func(n-(varq*(9**q)), c + varq)

        # print(varp, varq)


def main():
    n = int(input())
    global ans
    ans = 10**10
    c = 0

    func(n, c)
    print(ans)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N=int(input())
a,b,cnt=0,0,0
def nine():
    global a
    for i in range(N):
        if 9**i<=N:
            a=9**i
        else:
            break
def six():
    global b
    for i in range(N):
        if 6**i<=N:
            b=6**i
        else:
            break

for i in range(N):
    nine()
    six()
    N-=max(a,b)        
    cnt+=1
    if N==0:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:
N = int(input())
ArrayCardinals = [59049,46656,7776,6561,1296,729,216,81,36,9,6,1]
Residual = N
NumOperations = 0
for i in range(len(ArrayCardinals)):
  CurrentCardinal = ArrayCardinals[i]
  Quotient = Residual//CurrentCardinal
  if(Quotient>0):
    NumOperations += Quotient
    Residual = Residual%CurrentCardinal

Submission status: Wrong Answer

Solution code:
from math import log
from functools import lru_cache

n=int(input())

a=int(log(n,6))
b=int(log(n,9))
l=list(set([1]+[6**i for i in range(a)]+[9**i for i in range(b)]))

@lru_cache(maxsize=100000)
def func(n):
    if n<=0: return 1000000
    if n in l:return 1
    return min([func(n-li) for li in l])+1

print(func)

Submission status: Wrong Answer

Solution code:
n = int(input())
c = 0
a = sorted([9**5, 9**4, 9**3, 9**2, 9, 6**6, 6**5,
            6**4, 6**3, 6**2, 6, 1], reverse=True)
for i in a:
    if n >= i:
        c += n//i
        print(i, n//i, n)
        n -= (i*(n//i))

print(c)

Submission status: Wrong Answer

Solution code:
n = int(input())
money_list = (59049, 46656, 7776, 6561, 1296, 729, 216, 81, 36, 9, 6, 1)
ans = 0
for i in money_list:
  while n >= i:
    if (46656*2) <= n:
      n -= 46656
      ans += 1
    elif (6561*2) <= n < (7776+6561):
      n -= 6561
      ans += 1
    elif (729*2) <= n < (1296+729):
      n -= 729
      ans += 1
    elif (81*3) <= n <= (245):
      n -= 81
      ans += 1
    elif 12 <= n < 15:
      n -= 6
      ans += 1
    else:
      n -= i
      ans += 1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

def hikidashi(n):
    if 35 >= n:
        tmp1 = n//9 + (n%9)//6 + (n%9)%6
        tmp2 = n//6 + n%6

        if tmp1 >= tmp2:
            if n >= 6:
                return 6
            else:
                return 1
        else:
            return 9
    
    else:
        count = 0
        while True:
            tmp = pow(9, count)
            if tmp > n:
                break
            n9 = tmp
            count += 1

        count = 0
        while True:
            tmp = pow(6, count)
            if tmp > n:
                break
            n6 = tmp
            count += 1
        return max(n6, n9)


result = 0
while N != 0:
    result += 1
    N = N - hikidashi(N)
print(result)

Submission status: Wrong Answer

Solution code:
import math
N =int(input())

count=0
if N < 6:
    print(N)
else:
    while (1):
        n9 = math.log(N, 9)
        n6 = math.log(N, 6)
        n9,nn=math.modf(n9)
        n6, nj = math.modf(n6)
        #print(nn,nj)
        if math.pow(9, nn)>math.pow(6, nj):
            N -= math.pow(9, nn)
            #print('A')
        else:
            N -= math.pow(6, nj)
            #print('B')
        count += 1
        #print(N)
        if N < 6:
            print(int(count + N))
            break

Submission status: Wrong Answer

Solution code:
N=int(input())
a,b,cnt=0,0,0
def nine():
    global a
    for i in range(N):
        if 9**i<=N:
            a=9**i
        else:
            break
def six():
    global b
    for i in range(N):
        if 6**i<=N:
            b=6**i
        else:
            break

for i in range(N):
    nine()
    six()
    N-=max(a,b)        
    cnt+=1
    print(N,a,b,cnt)
    if N==0:
        break
print(cnt)

Submission status: Wrong Answer

Solution code:

N = int(input())


def solve():
    global N
    ans = 0
    a = []
    values = sorted([1] + [pow(6, _) for _ in range(1, 10)] + [pow(9, _) for _ in range(1, 10)])
    while N > 0:
        if 12 <= N <= 17:
            ans += 2 + (N - 12)
            break
        else:
            for i in range(len(values)):
                if N < values[i]:
                    ans += 1
                    a.append((values[i - 1], N))
                    N -= values[i-1]
                    break
                elif N == values[i]:
                    ans += 1
                    a.append((values[i], N))
                    N -= values[i]
                    break
            if N == 0:
                break
    return ans


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
import math
import bisect
nine=[]
six=[]
i=9
val=0
pos=1
def binary(n):
	s="{0:b}".format(n)
	return s
while(val<=100000):
	val=int(math.pow(i,pos))
	nine.append(val)
	pos=pos+1
i=6
pos=1
val=0
while(val<=100000):
	val=int(math.pow(i,pos))
	six.append(val)
	pos=pos+1
lis=six+nine
leng=len(lis)
binarr=[]
for j in range(0,int(math.pow(2,leng))):
	s1=binary(j)
	if(len(s1)!=leng):
		diff=leng-len(s1)
		for j in range(0,diff):
			s1='0'+s1
	binarr.append(s1[::-1])
ans=0
poss=[]
n=int(input())
for j in range(0,len(binarr)):
	sum=0
	count=0
	s1=binarr[j]
	for k in range(0,leng):
		if(s1[k]=='1'):
			count=count+1
			sum=sum+lis[k]
	poss.append(count+abs(n-sum))
	if(sum==n):
		break
poss.sort()
print(poss[0])

Submission status: Wrong Answer

Solution code:
N = int(input())


def solve():
    global N
    ans = 0
    a = []
    values = sorted([1] + [pow(6, _) for _ in range(1, 10)] + [pow(9, _) for _ in range(1, 10)])
    while N > 0:
        if 12 <= N <= 17:
            ans += 2 + (N - 12)
            break
        else:
            for i in range(len(values)):
                if N < values[i]:
                    ans += 1
                    a.append((values[i - 1], N))
                    N -= values[i-1]
                    break
                elif N == values[i]:
                    ans += 1
                    a.append((values[i], N))
                    N -= values[i]
                    break
            if N == 0:
                break
    print(a, sum([_ for _, __ in a]))
    return ans


if __name__ == '__main__':
    print(solve())

Submission status: Wrong Answer

Solution code:
import sys 
n=int(input())
ans=0
k=n%18
if n<6:
  print(n)
  sys.exit()
'''if 0<k<6:
  ans+=k
  n-=k
if 6<k<9:
  ans+=k-6
  n-=k-6
if 9<k<12:
  ans+=k-9
  n-=k-9
if 12<k<18:
  ans+=k-12
  n-=k-12'''
while True:
  i=0
  flg1=False
  flg2=False
  if n==0:
    break
  if n==1 or n==6 or n==9:
    ans+=1
    break
  if n==2 or n==7 or n==10 or n==12 or n==15:
    ans+=2
    break
  if n==3 or n==8 or n==11 or n==13 or n==16:
    ans+=3
    break
  if n==4 or n==14 or n==17:
    ans+=4
    break
  if n==5:
    ans+=5
    break
  while True:
    if 6**i>n and (not flg1):
      s=i-1
      flg1=True
    if 9**i>n and (not flg2):
      t=i-1
      flg2=True
    if flg1 and flg2:
      break
    i+=1
  n-=max(6**s,9**t)
  ans+=1

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = [1]
for i in range(1, 7):
  a.append(6**i)
  a.append(9**i)
a2 = sorted(list(set(a)))

ans = 0
for i in range(len(a2)-1, -1, -1):
    while N >= a2[i]:
        N -= a2[i]
        ans += 1
print(ans)