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;
// }
#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;
}


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

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

// Function to determine if a number can become divisible by 9
string uninteresting_numbers(const string &s)
{
    int n = s.length();
    // DP table: dp[i][sum] indicates if it's possible to achieve the `sum`
    // using digits up to index `i`.
    vector<vector<bool>> dp(n + 1, vector<bool>(9, false));

    // Base case: sum 0 is achievable with no digits used.
    dp[0][0] = true;

    // Iterate over each digit of the number
    for (int i = 0; i < n; i++)
    {
        int digit = s[i] - '0';
        for (int sum = 0; sum < 9; sum++)
        {
            // If `sum` is achievable using the first `i` digits
            if (dp[i][sum])
            {
                // Case 1: Do not modify this digit
                dp[i + 1][(sum + digit) % 9] = true;

                // Case 2: Replace the digit with its square (if <= 3)
                if (digit <= 3)
                {
                    int squared = digit * digit;
                    dp[i + 1][(sum + squared) % 9] = true;
                }
            }
        }
    }

    // Check if any sum divisible by 9 is achievable using all digits
    for (int sum = 0; sum < 9; sum++)
    {
        if (dp[n][sum] && sum == 0)
        {
            return "YES";
        }
    }
    return "NO";
}

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