Problem: 2050C - Uninteresting Number
Language: C++23 (GCC 14-64, msys2)

Previous submission (verdict: Time limit exceeded on test 6):
// Jai Ho mere Gurudev Bhagwan Ki

// Jai Jai Shree Radhey Shyam

#include<bits/stdc++.h>

#define int long long int

#define ll long long int

#define pb push_back

#define debug(...) __f (#__VA_ARGS__, __VA_ARGS__)

using namespace std;



/*B U G*/

template <typename Arg1>

void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }

template <typename Arg1, typename... Args>

void __f (const char* names, Arg1&& arg1, Args&&... args){

    const char* comma = strchr (names + 1, ',');

    cout.write (names, comma - names) << " : " << arg1 << " | ";

    __f (comma + 1, args...);

}

/*B U G*/

struct hash_pair {

    size_t operator()(const pair<int, int>& p) const {

        return hash<int>()(p.first) ^ (hash<int>()(p.second) << 1);

    }

};

unordered_map<pair<int, int>, bool, hash_pair> dp;

bool sol(int index, int sum, int n, vector<int> &vec){

    pair<int, int> key = {index, sum};

    if(index == n) return dp[key] = (sum%9 == 0);

    if(sum%9 == 0) return dp[key] = true;

    if(dp.find(key) != dp.end()) return dp[key];

    int real_val = vec[index], real_sum = sum;

    sum+=(vec[index]*(vec[index]-1)); vec[index]*=vec[index];

    if(sum%9 == 0) return dp[key] = true;

    if(sol(index+1, sum, n, vec)) return dp[key] = true;

    vec[index] = real_val, sum = real_sum;

    if(sol(index+1, sum, n, vec)) return dp[key] = true;



    return dp[key] = false;

}

void solve() {

    string s; cin>>s; int n = s.size(), sum = 0;

    vector<int> vec;

    for(int i = 0; i<n; ++i){

        int val = s[i] - '0'; sum+=val;

        if(val != 1 && val < 4) vec.push_back(val);

    }

    dp.clear();

    if(sol(0, sum, vec.size(), vec)) cout<<"YES\n";

    else cout<<"NO\n";

}



int32_t main(){

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

    #ifndef ONLINE_JUDGE

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

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

    #endif

    int t = 1;

    cin >> t;

    for(int i = 0; i < t; i++){

        solve();

    }

    return 0;

}

Submission (verdict: Accepted):
// Jai Ho mere Gurudev Bhagwan Ki

// Jai Jai Shree Radhey Shyam

#include<bits/stdc++.h>

#define int long long int

#define ll long long int

#define pb push_back

#define debug(...) __f (#__VA_ARGS__, __VA_ARGS__)

using namespace std;



/*B U G*/

template <typename Arg1>

void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }

template <typename Arg1, typename... Args>

void __f (const char* names, Arg1&& arg1, Args&&... args){

    const char* comma = strchr (names + 1, ',');

    cout.write (names, comma - names) << " : " << arg1 << " | ";

    __f (comma + 1, args...);

}

/*B U G*/

vector<vector<int>> dp;



bool sol(int index, int sum, int n, const vector<int> &vec) {

    if (index == n) return (sum % 9 == 0);

    if (sum % 9 == 0) return true;



    if (dp[index][sum % 9] != -1) return dp[index][sum % 9];



    // Option 1: Don't change vec[index]

    bool res = sol(index + 1, sum, n, vec);

    if (res) {

        dp[index][sum % 9] = true;

        return true;

    }



    // Option 2: Square vec[index] if possible

    int val = vec[index];

    int squared = val * val;

    if (squared < 10) {

        res = sol(index + 1, sum - val + squared, n, vec);

        if (res) {

            dp[index][sum % 9] = true;

            return true;

        }

    }



    dp[index][sum % 9] = false;

    return false;

}



void solve() {

    string s;

    cin >> s;

    int sum = 0;

    vector<int> vec;



    for (char c : s) {

        int val = c - '0';

        sum += val;

        if (val != 1 && val < 4) vec.push_back(val);

    }



    dp.assign(vec.size() + 1, vector<int>(9, -1));



    cout << (sol(0, sum, vec.size(), vec) ? "YES\n" : "NO\n");

}



int32_t main(){

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

    #ifndef ONLINE_JUDGE

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

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

    #endif

    int t = 1;

    cin >> t;

    for(int i = 0; i < t; i++){

        solve();

    }

    return 0;

}