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):
//package com.aakash.Contest.C991;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class C {
    static BufferedReader br = new BufferedReader(
            new InputStreamReader(System.in));

    public static void main(String[] args) throws IOException {

        StringTokenizer s1
                = new StringTokenizer(br.readLine());
        int t = Integer.parseInt(s1.nextToken());
        while (t-- > 0) {
            StringBuilder sb=new StringBuilder(br.readLine());
            int n = sb.length();
            boolean is=solve(sb,n);
            if(is) System.out.println("YES");
            else System.out.println("NO");
        }

    }

    static boolean solve(StringBuilder sb,int n) {
        int two=0,three=0;
        int sum=0;
        for(int i=0;i<n;i++) {
            int curr=sb.charAt(i)-'0';
            if(curr==2) two++;
            if(curr==3) three++;
            sum+=curr;
            sum%=9;
        }
        if((two>=5&&three>=5) ||sum==0) return true;
        sum=9-sum;
        for(int x=0;x<=two;x++) {
            for(int y=0;y<=three;y++) {
                long curr=x*2;
                curr+=y*6;
                if(curr%9==sum) return true;
            }
        }
        return false;
    }
}