Problem: 2050C - Uninteresting Number
Language: C++20 (GCC 13-64)

Previous submission (verdict: Time limit exceeded on test 3):
#include <bits/stdc++.h>
using namespace std;
// Check if a string represents a number divisible by 9
bool isDivisibleBy9(const string &s)
{
    int sum = 0;
    for (char c : s)
    {
        sum += (c - '0');
    }
    return (sum % 9 == 0);
}

// Recursive function to explore valid transformations
void solve(string s, bool &found, int i)
{
    if (found)
    {
        return;
    }
    // cout << s << endl;
    // Check if the current number is divisible by 9
    if (isDivisibleBy9(s))
    {
        found = true;
        return;
    }
    // If the current digit is <= 3, try replacing it with its square
    if (i < s.length())
    {
        if (s[i] <= '3')
        {
            char original = s[i];
            s[i] = '0' + (original - '0') * (original - '0');
            solve(s, found, i + 1);
            s[i] = original; // Backtrack
        }
        // Skip to the next digit
        solve(s, found, i + 1);
    }
}

// Wrapper function to determine if the number is interesting
string uninteresting_numbers(const string &s)
{
    bool found = false;
    string copy = s; // Create a mutable copy
    solve(copy, found, 0);
    return found ? "YES" : "NO";
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string number;
        cin >> number;
        cout << uninteresting_numbers(number) << endl;
    }
    return 0;
}


Submission (verdict: Time limit exceeded on test 3):
// #include <bits/stdc++.h>
// using namespace std;
// // Check if a string represents a number divisible by 9
// bool isDivisibleBy9(const string &s)
// {
//     int sum = 0;
//     for (char c : s)
//     {
//         sum += (c - '0');
//     }
//     return (sum % 9 == 0);
// }
// // Recursive function to explore valid transformations
// void solve(string s, bool &found, int i)
// {
//     if (found)
//     {
//         return;
//     }
//     // cout << s << endl;
//     // Check if the current number is divisible by 9
//     if (isDivisibleBy9(s))
//     {
//         found = true;
//         return;
//     }
//     // If the current digit is <= 3, try replacing it with its square
//     if (i < s.length())
//     {
//         if (s[i] <= '3')
//         {
//             char original = s[i];
//             s[i] = '0' + (original - '0') * (original - '0');
//             solve(s, found, i + 1);
//             s[i] = original; // Backtrack
//         }
//         // Skip to the next digit
//         solve(s, found, i + 1);
//     }
// }

// // Wrapper function to determine if the number is interesting
// string uninteresting_numbers(const string &s)
// {
//     bool found = false;
//     string copy = s; // Create a mutable copy
//     solve(copy, found, 0);
//     return found ? "YES" : "NO";
// }

// int main()
// {
//     int t;
//     cin >> t;
//     while (t--)
//     {
//         string number;
//         cin >> number;
//         cout << uninteresting_numbers(number) << endl;
//     }
//     return 0;
// }
#include <bits/stdc++.h>
using namespace std;

// Check if a string represents a number divisible by 9
bool isDivisibleBy9(const string &s)
{
    int sum = 0;
    for (char c : s)
    {
        sum += (c - '0');
    }
    return (sum % 9 == 0);
}

// Recursive function to explore valid transformations with memoization
bool solve(string s, int i, unordered_map<string, bool> &memo)
{
    // If already found a result for this state, return it
    string state = s + "_" + to_string(i);
    if (memo.find(state) != memo.end())
    {
        return memo[state];
    }

    // Check if the current number is divisible by 9
    if (isDivisibleBy9(s))
    {
        return memo[state] = true;
    }

    // If index exceeds the string length, return false
    if (i >= s.length())
    {
        return memo[state] = false;
    }

    // Try replacing the current digit if it is <= 3
    bool result = false;
    if (s[i] <= '3')
    {
        char original = s[i];
        s[i] = '0' + (original - '0') * (original - '0');
        result |= solve(s, i + 1, memo); // Recursive call after modification
        s[i] = original;                 // Backtrack
    }

    // Skip to the next digit without modifying
    result |= solve(s, i + 1, memo);

    // Store the result for this state and return it
    return memo[state] = result;
}

// Wrapper function to determine if the number is interesting
string uninteresting_numbers(const string &s)
{
    unordered_map<string, bool> memo; // Memoization map
    return solve(s, 0, memo) ? "YES" : "NO";
}

int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        string number;
        cin >> number;
        cout << uninteresting_numbers(number) << endl;
    }
    return 0;
}