Problem: 2050C - Uninteresting Number
Language: c++17 

Solution 1 (runtime: 124 ms):
//jai hanuman

#include<bits/stdc++.h>

using namespace std;

 

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)

#define MOD 1000000007

#define MOD1 998244353

#define INF 1e18

#define nline "\n"

#define pb push_back

#define ppb pop_back

#define mp make_pair

#define ff first

#define ss second

#define PI 3.141592653589793238462

#define set_bits __builtin_popcountll

#define sz(x) ((int)(x).size())

#define all(x) (x).begin(), (x).end()

 

typedef long long ll;

typedef unsigned long long ull;

typedef long double lld;

// typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update > pbds; // find_by_order, order_of_key

 

#ifndef ONLINE_JUDGE

#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;

#else

#define debug(x)

#endif

 

void _print(ll t) {cerr << t;}

void _print(int t) {cerr << t;}

void _print(string t) {cerr << t;}

void _print(char t) {cerr << t;}

void _print(lld t) {cerr << t;}

void _print(double t) {cerr << t;}

void _print(ull t) {cerr << t;}

 

template <class T, class V> void _print(pair <T, V> p);

template <class T> void _print(vector <T> v);

template <class T> void _print(set <T> v);

template <class T, class V> void _print(map <T, V> v);

template <class T> void _print(multiset <T> v);

template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}

template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}

template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}

 

bool solve(ll i, string &s, ll sum, ll n, vector<vector<int>> &dp) {

    if (i == n) {

        return (sum % 9 == 0);

    }



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

        return dp[i][sum];  

    }



    bool op1 = solve(i + 1, s, (sum + (s[i] - '0')) % 9, n, dp); 



    bool op2 = false;

    if (s[i] == '2') {

        op2 = solve(i + 1, s, (sum + 4) % 9, n, dp);

    } else if (s[i] == '3') {

        op2 = solve(i + 1, s, (sum + 9) % 9, n, dp);

    }



    return dp[i][sum] = (op1 || op2);  

}



int main() {

#ifndef ONLINE_JUDGE

    freopen("error.txt", "w", stderr);

    freopen("input.txt", "r", stdin);

    freopen("output.txt", "w", stdout);

#endif

    ios::sync_with_stdio(false);

    cin.tie(0);

    ll t;

    cin >> t;

    while (t--) {

        string s;

        cin >> s;

        ll n = s.size();

        vector<vector<int>> dp(n, vector<int>(10, -1));



        if (solve(0, s, 0, n, dp)) {

            cout << "YES" << endl;

        } else {

            cout << "NO" << endl;

        }

    }

    return 0;

}



Solution 2 (runtime: 61 ms):


#include<bits/stdc++.h>

using namespace std;

#define gc getchar_unlocked

#define fo(i,n) for(int i=0;i<n;i++)

#define Fo(i,k,n) for(int i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)

#define ll long long



#define all(x) x.begin(), x.end()

#define clr(x) memset(x, 0, sizeof(x))

#define sortall(x) sort(all(x))

#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)

#define PI 3.1415926535897932384626



vector< ll> primes, is_prime, spf;



void sieve( ll n);

ll expo(ll a, ll b); // gives a^b 

bool isPrime(ll num) ;





const  ll mod = 1'000'000'007;

const  ll N = 3e5, M = N;

//=======================





void solve() {



  string st;

  cin >> st;

  map <int, int> m;

  ll sum = 0;

  for (int i = 0; i < st.length(); i++) {

  	sum += (st[i]-48);

  	if (st[i] == '2') m[2]++;

  	if (st[i] == '3') m[3]++;

  }

  

  int rem = 9-(sum%9);

  if (rem == 9) {

  	cout << "YES" << endl;

  	return;

  }

  if (rem % 2 == 1) rem += 9; 

  //cout << rem;

  set <int> s;

  for (int i = 0; i <= m[2]; i++) {

  	if (i*2 > rem) break;

  	for (int j = 0; j <= m[3]; j++ ) {

  		int temp = 2*i+6*j;

  		if (temp > rem) break;

  		s.insert(temp);

  	}

  }

  for (int i = 0; i <= m[3]; i++) {

  	if (i*6 > rem) break;

  	for (int j = 0; j <= m[2]; j++ ) {

  		int temp = 6*i+2*j;

  		if (temp > rem) break;

  		s.insert(temp);

  	}

  }

  //for (int i : s) cout << i << " ";

  if (s.find(rem) != s.end()) {

  	cout << "YES" << endl;

  }

  else cout << "NO" << endl;







}



int main() {

    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

    srand(chrono::high_resolution_clock::now().time_since_epoch().count());



    int t = 1;

    cin >> t;

    while(t--) {

      solve();

    }



    return 0;

}













void sieve( ll n) {

    primes.clear();

    is_prime.assign(n + 1, 1);

    spf.assign(n + 1, 0);

    is_prime[0] = is_prime[1] = false;

    for (ll i = 2; i <= n; i++) {

        if (is_prime[i]) {

            primes.push_back(i);

            spf[i] = i;

            for (ll j = i * i; j <= n; j += i) {

                is_prime[j] = false;

                spf[j] = i;

            }

         }

     }

}



long long expo(long long a, long long b) {

    long long res = 1;

    while (b > 0) {

        if (b & 1)

            res = res * a;

        a = a * a;

        b >>= 1;

    }

    return res;

}



bool isPrime(ll num) {

    if (num <= 1) return false; // 1 or smaller are not prime

    if (num == 2) return true;  // 2 is the only even prime number

    if (num % 2 == 0) return false; // Other even numbers are not prime



    ll limit = static_cast<ll>(sqrt(num)); // We only need to check up to the square root

    for (ll i = 3; i <= limit; i += 2) { // Check odd numbers only

        if (num % i == 0) {

            return false; // Found a divisor, so not prime

        }

    }

    return true; // No divisors found, it is prime

}