Problem: 2050C - Uninteresting Number
Language: java

Solution 1 (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);
                }
            }
        }
    }
}


Solution 2 (runtime: 249 ms):
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class main {
    static PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
    static FastReader in = new FastReader();
    public static int n;  
    public static void main(String[] args) {
        int t = in.nextInt();
        while (t-- > 0) {
            String p = in.next();
            n = p.length();
            long sum = 0;
            for (int i = 0; i < n; i++) {
                int l = p.charAt(i) - '0';
                sum += l;
            }
            if (sum % 9 == 0) {
                pw.println("YES");
            } else {
                sum %= 9;
                sum = 9 - sum;

                int count2 = 0;
                int count3 = 0;
                for (int i = 0; i < n; i++) {
                    int l = p.charAt(i) - '0';
                    if (l == 2) {
                        count2++;
                    } else if (l == 3) {
                        count3++;
                    }
                }
                if (sum % 2 != 0) {
                    sum += 9;
                }
                while(count3 > 0 && sum >= 6) {
                    count3--;
                    sum -= 6;
                }
                while(count2 > 0 && sum >= 2) {
                    count2--;
                    sum -= 2;
                }
                if (sum == 0) {
                    pw.println("YES");
                } else {
                    pw.println("NO");
                }
            }
        }
        pw.close();
    }
    
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

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

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            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) {
                e.printStackTrace();
            }
            return str;
        }
    }
}