Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (runtime: 484 ms):
import java.util.*;

public class C_Uninteresting_Number{
    static HashMap<Integer, Boolean> hm;
    public static boolean func(int num, int two, int three){
        if(num%9==0)return true;
        if(two==0&&three==0)return false;
        if(hm.containsKey(num))return hm.get(num);
        boolean t=false, th=false;
        if(two>0){
            t = func(num+2, two-1, three);
        }
        if(three>0){
            th = func(num+6, two, three-1);
        }
        hm.put(num, t|th);
        return t|th;
    }
    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int t = scn.nextInt();
        while(t-->0){
            String s = scn.next();
            int n = s.length();
            int num = 0;
            int two = 0, three=0;
            for(int i = 0; i < n; i++){
                num += (s.charAt(i)-'0');
                if(s.charAt(i)=='2')two++;
                if(s.charAt(i)=='3')three++;
            }
            hm = new HashMap<>();
            boolean ans = func(num, two, three);
            if(ans)
            System.out.println("YES");
            else System.out.println("NO");
        }
    }
}

Solution 2 (runtime: 249 ms):

import java.io.*;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @author raito
 * @since 2024/12/26
 */
public class Cr_991 {
    public static void main(String[] args) {
        Fast fast = new Fast();
        new Cr_991().C(fast);
        fast.flush();
    }

    public void C(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            String s = fast.nextLine();
            long sum = 0;
            int a2 = 0;
            int a3 = 0;
            for (int i = 0; i < s.length(); i++) {
                int p = s.charAt(i) - '0';
                sum += p;
                if (p == 2) {
                    a2++;
                }
                if (p == 3) {
                    a3++;
                }
            }
            int d = (int) (sum % 9);
            if (d == 0) {
                fast.write("YES\n");
                continue;
            }
            // 6, 3
            // 2, 4, 6, 8, 1, 3, 5, 7, 0, 2, 4, 6, 8, 1, 3, 5, 7, 0
            int p = 9 - d;
            if (p == 2) {
                if (a2 > 0) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 4) {
                if (a2 > 1) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 3) {
                if (a3 >= 2 || (a3 == 1 && a2 >= 3 ) || a2 >= 6) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 1) {
                if (a2 >= 5 || (a3 >= 1 && a2 >= 2)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 5) {
                if (a2 >= 7 || (a3 >= 2 && a2 >= 1) || (a3 == 1 && a2 >= 4)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 6) {
                if (a3 >= 1 || a2 >= 3) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else if (p == 7) {
                if (a2 >= 8 || (a3 == 1 && a2 >= 5) || (a3 > 1 && a2 >= 2)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            } else {
                if (a2 >= 4 || (a3 >= 1 && a2 >= 1)) {
                    fast.write("YES\n");
                } else {
                    fast.write("NO\n");
                }
            }

        }
    }

    public void B(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            int n = fast.nextInt();
            long[] a = new long[n];
            long asum = 0;
            long bsum = 0;
            for (int i = 0; i < n; i++) {
                a[i] = fast.nextLong();
                if (((i + 1) & 1) == 1) {
                    asum += a[i];
                } else {
                    bsum += a[i];
                }
            }
            long sum = asum + bsum;
            if (sum % n != 0) {
                fast.write("NO\n");
                continue;
            }
            long target = sum / n;
            int blen = n / 2;
            int alen = n - blen;
            if (asum != alen * target || bsum != target * blen) {
                fast.write("NO\n");
            } else {
                fast.write("YES\n");
            }
        }
    }

    public void A(Fast fast) {
        int t = fast.nextInt();
        while(--t >= 0) {
            int n = fast.nextInt();
            int m = fast.nextInt();
            int ans = 0;
            for(int i = 0; i < n; i++) {
                String s = fast.nextLine();
                if (s.length() <= m) {
                    m -= s.length();
                    ans++;
                } else {
                    m = -1;
                }
            }
            fast.write(ans + "\n");
        }
    }

    public static class Fast {
        BufferedReader br;
        StringTokenizer st;
        BufferedWriter bw;

        public Fast() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
            return str;
        }

        void write(String s) {
            if (bw == null) {
                bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
            try {
                bw.write(s);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        void write(int c) {
            if (bw == null) {
                bw = new BufferedWriter(new OutputStreamWriter(System.out));
            }
            try {
                bw.write(c);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        void flush() {
            if (bw != null) {
                try {
                    bw.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}