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: 249 ms):
for i in range(int(input())):
    n=(input())
    a=0
    for i in n:
        a+=int(i)
    a=a%9
    a=9-a
    if a==9:
        print("YES")
    else:
        if a%2!=0:
            p=0
            q=0
            for i in n:
                if i=="2":
                    p+=1
                if i=="3":
                    q+=1
            if a==1:
                if p>=5 or (p>=2 and q>=1):
                    print("YES")
                else:
                    print("NO")
            elif a==3:
                if q>=2 or p>=6 or (q>=1 and p>=3):
                    print("YES")
                else:
                    print("NO")
            elif a==5:
                if p>=7 or (q>=1 and p>=4) or (q>=2 and p>=1):
                    print("YES")
                else:
                    print('NO')
            else:
                if p>=8 or (q>=1 and p>=5) or (q>=2 and p>=2):
                    print("YES")
                else:
                    print("NO")
        else:
            if a<6:
                a=a//2
                ans=0
                for i in n:
                    if i=="2":
                        ans+=1
                if ans>=a:
                    print("YES")
                else:
                    print("NO")
            else:
                if a==6:
                    a=a//2
                    ans=0
                    for i in n:
                        if i=="2":
                            ans+=1
                    if ans>=a:
                        print("YES")
                    else:
                        for i in n:
                            if i=="3":
                                print("YES")
                                break
                        else:
                            print("NO")
                else:
                    a=a//2
                    ans=0
                    for i in n:
                        if i=="2":
                            ans+=1
                    if ans>=a:
                        print("YES")
                    else:
                        if ans<1:
                            print("NO")
                        else:
                            for i in n:
                                if i=="3":
                                    print("YES")
                                    break
                            else:
                                print("NO")