Problem: 2050C - Uninteresting Number
Language: C++17 (GCC 7-32)

Previous submission (verdict: Time limit exceeded on test 3):
#include<bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define vll vector<long long>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<long long,long long>>
#define mod1 1000000007
#define mod2 998244353
#define INF 1e18
#define pb push_back
#define ppb pop_back
#define mkp 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()
#define mn(nums) *min_element(nums.begin(),nums.end()) 
#define mx(nums) *max_element(nums.begin(),nums.end())
#define rep(i,a,b) for(ll i=(a) ; i<(b) ; i++)
#define read(a) for(auto &it : a) cin>>it
#define ll long long
#define yes cout << "YES" << endl 
#define no cout << "NO" << endl 
#define nl endl 
// Memoization map

bool isprime(ll x)
{
	if(x == 2) return true ;
	for(int i=2;i<x;i++)
	{
		if(x%i == 0) return false ;
	}
	return true ;
}
ll cell(ll a,ll b)
{
	ll res = a/b ;
	if(a%b) res++ ;
	return res ;
}

bool solver(vector<ll> &v, map< pair<ll,ll> , ll>&dp, ll sum, ll ind)
{
	if(ind >= v.size())
	{
		if(sum%9 == 0) return true ;
		return false ;
	}
	
	
	if(dp[{ind,sum}]) return dp[{ind,sum}] ;
	
	bool take = false , nottake = false ;
	if(v[ind] == 2 or v[ind] == 3) 
	{
		ll a = v[ind] ;
		take = solver(v, dp, sum+a*a-a, ind+1) ;
	}
	nottake = solver(v, dp, sum, ind+1) ;
	
	return dp[{ind,sum}] = take or nottake ;
	
}
void solve() {   
    
        string s ;
        cin>>s ;
        vll v ;
        for(auto &it : s) v.pb(it-'0') ;
        
        ll sum = accumulate(all(v),0LL) ;
        
        map< pair<ll,ll> , ll> dp ;
        
        bool check = solver(v, dp, sum, 0) ;
        if(check) yes ;
        else no ;
    
        	
        
    }
 
 

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif

    ll t=1 ;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Submission (verdict: Accepted):
#include<bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define vll vector<long long>
#define vpii vector<pair<int,int>>
#define vpll vector<pair<long long,long long>>
#define mod1 1000000007
#define mod2 998244353
#define INF 1e18
#define pb push_back
#define ppb pop_back
#define mkp 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()
#define mn(nums) *min_element(nums.begin(),nums.end()) 
#define mx(nums) *max_element(nums.begin(),nums.end())
#define rep(i,a,b) for(ll i=(a) ; i<(b) ; i++)
#define read(a) for(auto &it : a) cin>>it
#define ll long long
#define yes cout << "YES" << endl 
#define no cout << "NO" << endl 
#define nl endl 
// Memoization map

bool isprime(ll x)
{
	if(x == 2) return true ;
	for(int i=2;i<x;i++)
	{
		if(x%i == 0) return false ;
	}
	return true ;
}
ll cell(ll a,ll b)
{
	ll res = a/b ;
	if(a%b) res++ ;
	return res ;
}

bool solver(vector<ll> &v, vector<vector<ll>>&dp, ll sum, ll ind)
{
	if(ind >= v.size())
	{
		if(sum == 0) return true ;
		return false ;
	}
	
	
	if(dp[ind][sum] != -1) return dp[ind][sum] ;
	
	bool take = false , nottake = false ;
	if(v[ind] == 2 or v[ind] == 3) 
	{
		ll a = v[ind] ;
		take = solver(v, dp, (sum+a*a-a)%9, ind+1) ;
	}
	nottake = solver(v, dp, sum%9, ind+1) ;
	
	return dp[ind][sum] = take or nottake ;
	
}
void solve() {   
    
        string s ;
        cin>>s ;
        vll v ;
        for(auto &it : s) v.pb(it-'0') ;
        
        ll sum = (accumulate(all(v),0LL))%9 ;
        
        vector<vector<ll>> dp(v.size(),vector<ll>(9,-1)) ;
        
        bool check = solver(v, dp, sum, 0) ;
        if(check) yes ;
        else no ;
    
        	
        
    }
 
 

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    #ifndef ONLINE_JUDGE
    freopen("Error.txt", "w", stderr);
    #endif

    ll t=1 ;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}