Problem: 2050C - Uninteresting Number
Language: java

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

public class Main{

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

		int t = Integer.parseInt(br.readLine());

		while(t-- > 0){
			String num = br.readLine();
			char[] s = num.toCharArray();
			boolean[] dp = new boolean[9];
			dp[0] = true;

			for(char c: s){
				boolean[] ndp = new boolean[9];
				for(int i = 0; i < 9; i++){
					if(dp[i]){
						int digit = c - '0';
						while(digit <= 9){
							ndp[(i+digit)%9] = true;
							if(digit <= 1) break;
							digit *= digit;
						}
					}
				}
				dp = ndp;
			}

			bw.write(dp[0] ? "YES\n" : "NO\n");

		}

		bw.flush();
		br.close();
		bw.close();
		
	}

}

Solution 2 (runtime: 296 ms):


import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.*;

 

public class Main {

    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 {

                if (st.hasMoreTokens()) {

                    str = st.nextToken("\n");

                }

                else {

                    str = br.readLine();

                }

            }

            catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }

    public static int check(ArrayList<Integer> arr,int i,int sum,int k,int dp[][]){

            

            if(i==arr.size()){

                return (sum+k)%9==0?1:0;

            }

            if(dp[i][sum]==-1){

            int take= check(arr,i+1,(sum+arr.get(i))%9,k,dp);

            int ntake=check(arr,i+1,sum,k,dp);

             dp[i][sum]= (take+ntake)>0?1:0;

            }

            return dp[i][sum];

    }

    public static boolean helper(ArrayList<Integer> arr,int k){

        int n=arr.size();

        int dp[][]=new int[n][10];

       

            for(int a[]:dp){

                Arrays.fill(a,-1);

            }

        return check(arr,0,0,k,dp)>0;

    }

    public static void solve(FastReader sc){

        String s=sc.next(); 

        int sum=0;

        ArrayList<Integer> arr=new ArrayList<>();

        for(char c:s.toCharArray()){

            int num=c-'0';

            sum=(sum+num)%9;

            if(num==2){

                arr.add(2);

            }

            else if(num==3){

                arr.add(6);

            }

        }

        if(sum!=0){

           if(helper(arr,sum)){

            System.out.println("YES");

           }

           else{

            System.out.println("NO");

           }

        }

        else{

            System.out.println("YES");

        }





    }

    



    

    

    public static void main(String args[]) {

        FastReader sc = new FastReader();

        int t=sc.nextInt();

        while(t-- >0){

            solve(sc);

        }

    }

}