ShaysXIA/CodeInsightBench huggingface dataset:
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 171 ms):

def digit_sum(n):
    if n[0] == '-':
        return digit_sum(n[1:])
    counter = 0
    for i in n:
        counter += int(i)
    return counter

def check(n):
    k = digit_sum(n)
    if k % 9 == 0:
        return 'YES'
    n_2 = n.count('2')
    n_3 = n.count('3')
    seen_2 = set()
    for i in range(n_2+1):
        if (i * 2) % 9 in seen_2:
            break
        seen_2.add((i*2)%9)
        for j in range(n_3 + 1):
            if n[0] != '-':
                if (k + i * 2 + j * 6) % 9 == 0:
                    return 'YES'
            else:
                if (k - i * 2 - j * 6 % 9) == 0:
                    return 'YES'
    return 'NO'


for i in range(int(input())):
    print(check(input()))
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 186 ms):
for _ in range(int(input())):
    n = input()
    
    sm = 0
    for i in str(n):
        sm += int(i)

    twos = n.count('2')
    threes = n.count('3')
    
    ans = False
    for x in range(min(twos+1, 9)):
        for y in range(min(threes+1, 9)):
            if (sm + (2*x) + (6*y))%9 == 0:
                ans = True

    print("YES" if ans else "NO")
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 171 ms):
def solve(num):
    three = 0
    two = 0
    s = 0
    for i in num:
        if i == '2':
            two += 1
        elif i == '3':
            three += 1
        s += int(i)

    if s % 9 == 0:
        return "YES"

    for i in range(min(9,two+1)):
        for j in range(min(7,three+1)):
            if (s + 2*i + 6*j ) % 9 == 0:
                return "YES"
    return "NO"




for _ in range(int(input())):
    print(solve(input()))
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 156 ms):
no = int(input())
for i in range(no):
    l = list(map(int, list(input())))
    c2 = l.count(2)
    c3 = l.count(3)
    sumo = sum(l)
    ans = "NO"
    for two in range(c2+1):
        for three in range(c3+1):
            x = sumo + 2*two + 6*three
            if x%9==0:
                ans = "YES"
                break
        if ans == "YES":
            break
    print(ans)
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 140 ms):
import sys
input=lambda :sys.stdin.readline().strip()
t=int(input())
for _ in range(t):
    s=input()
    ls=[int(i) for i in s]
    a=s.count('2')
    b=s.count('3')
    flag=0
    for i in range(min(10,a+1)):
        for j in range(min(10,b+1)):
            if (sum(ls)+2*i+6*j)%9==0:
                flag+=1
                print("YES")
                break
        if flag!=0:
            break
    if flag==0:
        print("NO")
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 124 ms):
def main():
    import sys
    input = sys.stdin.read
    data = input().split()

    tc = int(data[0])  # Read the number of test cases
    results = []

    for t in range(1, tc + 1):
        str_num = data[t]
        sum_digits = 0
        two_cnt = 0
        three_cnt = 0

        # Count sum of digits, number of 2's, and number of 3's
        for char in str_num:
            digit = int(char)
            sum_digits += digit
            if digit == 2:
                two_cnt += 1
            elif digit == 3:
                three_cnt += 1

        valid = False

        # Try combinations of 2's and 3's
        for i in range(min(8, two_cnt) + 1):
            for j in range(min(8, three_cnt) + 1):
                if (sum_digits + 2 * i + 6 * j) % 9 == 0:
                    results.append("yes".capitalize())
                    valid = True
                    break
            if valid:
                break
        if not valid:
            results.append("no".capitalize())
    
    # Print all results at once
    sys.stdout.write("\n".join(results) + "\n")

if __name__ == "__main__":
    main()
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 202 ms):
import sys
sys.set_int_max_str_digits(10**6)
t=int(input())
for _ in range(t):
    n=int(input())
    s=str(n)
    x=0
    for i in range(len(s)):
        x+=int(s[i])
    a,b=0,0
    for i in range(len(s)):
        if s[i]=="2":
            a,b=a+1,b
        elif s[i]=="3":
            a,b=a,b+1
        else:
            a,b=a,b
    i=0
    while i<a+1:
        j=0
        while j<b+1:
            if (x+2*i+6*j)%9==0:
                break
            else:
                j+=1
        if j<b+1:
            if (x+2*i+6*(j))%9==0:
                break
            else:
                i+=1
        else:
            if (x+2*i+6*(j-1))%9==0:
                break
            else:
                i+=1
    if i==a+1 and j==b+1:
        print("NO")
    else:
        print("YES")
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 171 ms):
def voir(test,nb3):
    while test!=0 and nb3>0:
        test=(test+6)%9
        nb3-=1
    if test==0:
        return True
    else :
        return False

OUT=[]
for i in range(int(input())):
    s=input()
    somme=0
    nb2=0
    nb3=0
    for i in s:
        somme=(somme+int(i))%9
        if i=="2":
            nb2+=1 
        if i=="3":
            nb3+=1
    
    if somme==0 :
        OUT.append("YES")
    else :
        
        
        if nb2>=2 :
            
            if voir((somme+4)%9,(nb3+(nb2-2)//3)) or voir((somme+2)%9,(nb3+(nb2-1)//3)) or voir((somme)%9,(nb3+(nb2)//3)):
                OUT.append("YES")
            else :
                OUT.append("NO")
        elif nb2==1 :
            
            if voir((somme+2)%9,(nb3+(nb2-1)//3)) or voir((somme)%9,(nb3+(nb2)//3)):
                OUT.append("YES")
            else :
                OUT.append("NO")
        else:
            
            if voir((somme)%9,(nb3+(nb2)//3)):
                OUT.append("YES")
            else :
                OUT.append("NO")


for i in OUT:
    print(i)
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 156 ms):
t = int(input())
for i in range(t):
	n = input()
	if int(n)%9 == 0:
		print('yes')
		continue	
	tw = n.count('2')
	th = n.count('3')
	n = int(n)
	tn = abs(0.5*((n%9)%2)*(9-n%9)+(abs(n%9)%2-1)*(9-n%9/2))
	d = tn - tw
	if d > 0 and ((d%3!=0 and (tw<3-d%3 or (d+3-d%3)/3 > th)) or (d%3==0 and d/3 > th)):
		print('no')
	else:
		print('yes')
Problem: 2050C - Uninteresting Number
Language: python

Solution 1 (runtime: 249 ms):
# ======= author: kuanhungchen.com | created: 05/02/23 08:33:06 CST ======= #
from sys            import stdin,  stderr, stdout, setrecursionlimit
from bisect         import bisect_left, bisect_right
from collections    import defaultdict, deque, Counter
from itertools      import accumulate, combinations, permutations, product
from functools      import lru_cache, cmp_to_key, reduce
from heapq          import heapify, heappush, heappop, heappushpop, heapreplace
# from pypyjit        import set_param
# set_param("max_unroll_recursion=-1")
# setrecursionlimit(300005)
orda  = 97
ordA  = 65
INF   = 1 << 60
MOD   = 1000000007
MOD2  = 998244353
input = lambda: stdin.readline().rstrip("\r\n")
linen = lambda cf_: "[L" + str(cf_.f_back.f_lineno) + "] "
dbg   = lambda *A, **M: stderr.write("\033[91m" + \
        M.get("sep", " ").join(map(str, A)) + M.get("end", "\n") + "\033[0m")
# ============================ START OF MY CODE ============================ #

def solve(_tc):
    s = input()
    n = len(s)
    dp = [set() for _ in range(n)]
    x = int(s[0])
    while x <= 9:
        dp[0].add(x % 9)
        if x == 0 or x == 1: break
        x *= x
    for i in range(1, n):
        x = int(s[i])
        xs = []
        while x <= 9:
            xs.append(x)
            if x == 0 or x == 1: break
            x *= x
        st = set()
        for d in dp[i - 1]:
            for x in xs:
                st.add((d + x) % 9)
        dp[i] = st
    if 0 in dp[-1]:
        print("YES")
    else:
        print("NO")


if __name__ == "__main__":
    _tcs = int(input())
    for _tc in range(1, vars().get("_tcs", 1) + 1):
        dbg("=== Case {} ===".format(str(_tc).rjust(2)))
        solve(_tc)


Solution 2 (runtime: 218 ms):
for _ in range(int(input())):
    s=[int(x) for x in list(input())]
    sm=sum(s)
    t=s.count(2)
    th=s.count(3)
    found=False
    for i in range(t+1):
        for j in range(th+1):
            if (sm+i*2+j*6)%9==0:
                print('YES')
                found=True
                break
        if found:
            break
    if not found:
        print('NO')