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

Previous submission (verdict: Memory limit exceeded on test 4):
//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 << "]";}

 ll n;

vector<vector<char>> dp;  // Using char to save memory (1 byte per state)



bool solve(ll i, string s, ll sum) {

    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); 



    bool op2 = false;

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

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

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

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

    }



    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

    fastio();

    ll t;

    cin >> t;

    while (t--) {

        string s;

        cin >> s;

        n = s.size();



        // Only 9 columns for the modulo results [0-8]

        dp.assign(n, vector<char>(9, -1));



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

            cout << "YES" << endl;

        } else {

            cout << "NO" << endl;

        }

    }

    return 0;

}



Submission (verdict: Accepted):
//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;

}