#include <algorithm>

#include <bits/stdc++.h>

#include <vector>

using namespace std;

#define ll long long

int const mod = 998244353, N = 2e5 + 10;

#define fast                                                                   \

  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

int dp[N][13];

int n;

string s;

int rec(int i, int sum) {

  if (sum % 9 == 0) {

    return 1;

  }

  if (i == n) {

    return 0;

  }

  int &ret = dp[i][sum];

  sum %= 9;

  int op1 = 0, op2 = 0, op3 = 0;

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

    op1 = rec(i + 1, sum + 2);

  }

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

    op2 = rec(i + 1, sum + 6);

  }

  op3 = rec(i + 1, sum);

  return ret = op1 | op2 | op3;

}

int main() {

  fast;

  memset(dp, -1, sizeof dp);

  int t;

  cin >> t;

  while (t--) {

    // cin >> n;

    cin >> s;

    n = s.size();

    int sum = 0;

    for (int i = 0; i < s.size(); i++) {

      sum += (s[i] - '0');

    }

    cout << (rec(0, sum) ? "YES" : "NO") << '\n';

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

      for (int j = 0; j <= 9; j++) {

        dp[i][j] = -1;

      }

    }

  }

}

#include <algorithm>

#include <bits/stdc++.h>

#include <vector>

using namespace std;

#define ll long long

int const mod = 998244353, N = 2e5 + 10;

#define fast                                                                   \

  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)

int dp[N][10];

int n;

string s;

int rec(int i, int sum) {

  if (i == n) {

    return (sum % 9 == 0);

  }

  if (sum % 9 == 0) {

    return 1;

  }

  sum %= 9;

  int &ret = dp[i][sum];

  if(~ret){

    return ret;

  }

  ret = 0;

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

    ret |= rec(i + 1, sum + 2);

  }

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

    ret |= rec(i + 1, sum + 6);

  }

  ret |= rec(i + 1, sum);

  return ret;

}

int main() {

  fast;

  memset(dp, -1, sizeof dp);

  int t;

  cin >> t;

  while (t--) {

    // cin >> n;

    cin >> s;

    n = s.size();

    int sum = 0;

    for (int i = 0; i < s.size(); i++) {

      sum += (s[i] - '0');

    }

    cout << (rec(0, sum % 9) ? "YES" : "NO") << '\n';

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

      for (int j = 0; j <= 9; j++) {

        dp[i][j] = -1;

      }

    }

  }

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

int main(){
    int t, sum;
    vector<int> possibilities;
    map<int, int> found;
    string n;
    cin >> t;
    while(t--){
        cin >> n;
        sum = 0; found.clear();
        for (auto i : n){
            sum += i-48;
            if (i == '2')
                found[2]++;
            if (i == '3')
                found[3]++;
        }
        for(int i = 0; i < min(9, found[2]+1); i++){
            for (int j = 0; j < min(9, found[3]+1); j++){
                if ((sum + i*2 + j*6)%9 == 0){
                    cout << "YES" << endl;
                    found[0]++;
                    break;
                }
            }
            if (found[0])
                break;
        }
        if (!found[0])
            cout << "NO" << endl;
    }
}

#include <bits/stdc++.h>
using namespace std;

int main(){
    int t, sum;
    vector<int> possibilities;
    map<int, int> found;
    string n;
    cin >> t;
    while(t--){
        cin >> n;
        sum = 0; found.clear();
        for (auto i : n){
            sum += i-48;
            if (i == '2')
                found[2]++;
            if (i == '3')
                found[3]++;
        }
        for(int i = 0; i < min(8, found[2]+1); i++){
            for (int j = 0; j < min(8, found[3]+1); j++){
                if ((sum + i*2 + j*6)%9 == 0){
                    cout << "YES" << endl;
                    found[0]++;
                    break;
                }
            }
            if (found[0])
                break;
        }
        if (!found[0])
            cout << "NO" << endl;
    }
}

Answer: 1


// ॐ
//---------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define endll '\n'
#define print(x) cout << x << endll
#define py cout << "YES\n"
#define pn cout << "NO\n"
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define fer(i, a, b) for (ll i = a; i <= b; i++)
#define frr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define int int64_t
#define all(v) v.begin(), v.end()
#define all_rev(v) v.rbegin(), v.rend()
#define vecp(v)           \
    for (auto i : v)      \
        cout << i << ' '; \
    cout << endll
#define vecpp(v)     \
    for (auto i : v) \
    cout << i.ff << ' ' << i.ss << endl
#define take(x) \
    int x;      \
    cin >> x
#define popcnt(n) __builtin_popcountll(n)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define debug(x) cout << #x << ' ' << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvll = vector<vll>;
using stll = set<ll>;
using mpll = map<ll, ll>;
using mpvll = map<ll, vll>;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 2e5 + 10;

// priority_queue <int, vector<int>, greater<int> > p;
//  __builtin_clz(x): the number of zeros at the beginning of the number
//  __builtin_ctz(x): the number of zeros at the end of the number
//  __builtin_popcountll(x): the number of ones in the number
//  __builtin_parity(x): the parity (even or odd) of the number of ones
//  __builtin_ffs(int) finds the index of the first (most right) set bit
void init_code()
{
    fast_io;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
    freopen("error.txt", "w", stderr);
#endif
}
bool isPrime(const ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 or n % 3 == 0)
        return false;
    for (ll i = 5; i * i <= n; i += 6)
        if (n % i == 0 or n % (i + 2) == 0)
            return false;
    return true;
}
ull power(ll x, ll y)
{
    ull res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % MOD;
        x = (x * x) % MOD;
        y >>= 1;
    }
    return res;
}
ull modInvPow(const ll n)
{
    return power(n, MOD - 2);
}
bool sortByCond(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
    /*
        if (a.ff == b.ff)
            return a.ss < b.ss;
        else
            return a.ff < b.ff;
    */
    return a.ss < b.ss;
}

void ramram()
{
    string s;
    cin >> s;
    int sum = 0;
    int cnt2 = 0, cnt3 = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '2')
            cnt2++;
        if (s[i] == '3')
            cnt3++;
        sum += s[i] - '0';
    }
    int rem = sum % 9;
    rem = 9 - rem;
    // cout << cnt2 << " " << cnt3 << " " << rem << endl;
    if (sum % 9 == 0)
    {
        py;
        return;
    }
    if (cnt2 == 0 && cnt3 == 0)
    {
        pn;
        return;
    }
    if (cnt2 >= 2 && cnt3 >= 2)
    {
        py;
        return;
    }
    if (cnt2 == 1 && cnt3 >= 1)
    {
        int tt = sum;
        if ((tt + 2) % 9 == 0)
        {
            py;
            return;
        }
        for (int i = 0; i < cnt3; i++)
        {
            tt += 6;
            if (tt % 9 == 0 || (tt + 2) % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt2 >= 1 && cnt3 == 1)
    {
        int tt = sum;
        if ((tt + 6) % 9 == 0)
        {
            py;
            return;
        }
        for (int i = 0; i < cnt2; i++)
        {
            tt += 2;
            if (tt % 9 == 0 || (tt + 6) % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt2 == 0)
    {
        int tt = sum;
        for (int i = 0; i < cnt3; i++)
        {
            tt += 6;
            if (tt % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt3 == 0)
    {
        int tt = sum;
        for (int i = 0; i < cnt2; i++)
        {
            tt += 2;
            if (tt % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    cout << "hii" << endl;
    return;
}

int32_t main()
{
    init_code();
    fast_io;
    ll t = 1;
    cin >> t;
    while (t--)
        ramram();
    return 0;
}

// ॐ
//---------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <bits/stdc++.h>
#define fast_io                       \
    ios_base::sync_with_stdio(false); \
    cin.tie(NULL);
#define endll '\n'
#define print(x) cout << x << endll
#define py cout << "YES\n"
#define pn cout << "NO\n"
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define fer(i, a, b) for (ll i = a; i <= b; i++)
#define frr(i, a, b) for (ll i = a; i >= b; i--)
#define pb push_back
#define ff first
#define ss second
#define sz size()
#define int int64_t
#define all(v) v.begin(), v.end()
#define all_rev(v) v.rbegin(), v.rend()
#define vecp(v)           \
    for (auto i : v)      \
        cout << i << ' '; \
    cout << endll
#define vecpp(v)     \
    for (auto i : v) \
    cout << i.ff << ' ' << i.ss << endl
#define take(x) \
    int x;      \
    cin >> x
#define popcnt(n) __builtin_popcountll(n)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define mxe(v) *max_element(v.begin(), v.end())
#define mne(v) *min_element(v.begin(), v.end())
#define debug(x) cout << #x << ' ' << x << endl
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
using vll = vector<ll>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vvll = vector<vll>;
using stll = set<ll>;
using mpll = map<ll, ll>;
using mpvll = map<ll, vll>;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
const ll N = 2e5 + 10;

// priority_queue <int, vector<int>, greater<int> > p;
//  __builtin_clz(x): the number of zeros at the beginning of the number
//  __builtin_ctz(x): the number of zeros at the end of the number
//  __builtin_popcountll(x): the number of ones in the number
//  __builtin_parity(x): the parity (even or odd) of the number of ones
//  __builtin_ffs(int) finds the index of the first (most right) set bit
void init_code()
{
    fast_io;
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
#ifndef ONLINE_JUDGE
    freopen("error.txt", "w", stderr);
#endif
}
bool isPrime(const ll n)
{
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
    if (n % 2 == 0 or n % 3 == 0)
        return false;
    for (ll i = 5; i * i <= n; i += 6)
        if (n % i == 0 or n % (i + 2) == 0)
            return false;
    return true;
}
ull power(ll x, ll y)
{
    ull res = 1;
    while (y > 0)
    {
        if (y & 1)
            res = (res * x) % MOD;
        x = (x * x) % MOD;
        y >>= 1;
    }
    return res;
}
ull modInvPow(const ll n)
{
    return power(n, MOD - 2);
}
bool sortByCond(const pair<ll, ll> &a, const pair<ll, ll> &b)
{
    /*
        if (a.ff == b.ff)
            return a.ss < b.ss;
        else
            return a.ff < b.ff;
    */
    return a.ss < b.ss;
}

void ramram()
{
    string s;
    cin >> s;
    int sum = 0;
    int cnt2 = 0, cnt3 = 0;
    for (int i = 0; i < s.size(); i++)
    {
        if (s[i] == '2')
            cnt2++;
        if (s[i] == '3')
            cnt3++;
        sum += s[i] - '0';
    }
    int rem = sum % 9;
    rem = 9 - rem;
    // cout << cnt2 << " " << cnt3 << " " << rem << endl;
    if (sum % 9 == 0)
    {
        py;
        return;
    }
    if (cnt2 == 0 && cnt3 == 0)
    {
        pn;
        return;
    }
    if (cnt2 >= 2 && cnt3 >= 2)
    {
        py;
        return;
    }
    if (cnt2 == 1 && cnt3 >= 2)
    {
        int tt = sum;
        for (int i = 0; i < cnt3; i++)
        {
            tt += 6;
            if (tt % 9 == 0 || (tt + 2) % 9 == 0)
            {
                py;
                return;
            }
        }
        cout<<"chut"<<endl;
        return;
    }
    if (cnt2 >= 2 && cnt3 == 1)
    {
        int tt = sum;
        for (int i = 0; i < cnt2; i++)
        {
            tt += 2;
            if (tt % 9 == 0 || (tt + 6) % 9 == 0)
            {
                py;
                return;
            }
        }
       cout<<"chut2"<<endl;
        return;
    }
    if (cnt2 == 1 && cnt3 == 1)
    {
        if (rem == 2 || rem == 6 || rem == 8)
        {
            py;
            return;
        }
        else
        {
            pn;
            return;
        }
    }
    if (cnt2 == 0)
    {
        int tt = sum;
        for (int i = 0; i < cnt3; i++)
        {
            tt += 6;
            if (tt % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    if (cnt3 == 0)
    {
        int tt = sum;
        for (int i = 0; i < cnt2; i++)
        {
            tt += 2;
            if (tt % 9 == 0)
            {
                py;
                return;
            }
        }
        pn;
        return;
    }
    cout<<"hii"<<endl;
    return;
}

int32_t main()
{
    init_code();
    fast_io;
    ll t = 1;
    cin >> t;
    while (t--)
        ramram();
    return 0;
}

Answer: 1


Answer: 2


#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

int dp[100005][10];
bool f(int i, int cur, vector<int> &v, int sum)
{
    if (i >= v.size())
        return 0;
    // dbg(dp[i][cur]);

    if (dp[i][cur] != -1)
        return dp[i][cur];
    int x = cur;
    cur += v[i];
    if ((cur % 9) == (9 - sum))
        return 1;
    x %= 9;
    cur %= 9;
    return dp[i][cur] = f(i + 1, cur, v, sum) || f(i + 1, x, v, sum);
}
void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> h;
    int sum = 0;
    vector<int> v;
    for (char &c : h)
    {
        int x = c - '0';
        if (x <= 3)
        {
            if (x == 2)
                v.push_back(2);
            if (x == 3)
                v.push_back(6);
        }
        sum += x;
    }
    sum %= 9;
    dbg(sum, v);
    if (!sum)
    {
        cout << "YES" << endl;
        return;
    }
    memset(dp, -1, sizeof(dp));
    if (f(0, 0, v, sum))
    {
        cout << "YES" << endl;
    }
    else
        cout << "NO" << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

#include "bits/stdc++.h"
#ifndef ONLINE_JUDGE
#include "debug.h"
#else
#define dbg(x...)
#endif
using namespace std;
using ll = long long;
#define int ll
#define endl '\n'
const int mod = 1000000007;
// clang-format off
template<typename typC,typename typD> istream &operator>>(istream &cin,pair<typC,typD> &a) { return cin>>a.first>>a.second; }
template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (auto &x:a) cin>>x; return cin; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const pair<typC,typD> &a) { return cout<<a.first<<' '<<a.second; }
template<typename typC,typename typD> ostream &operator<<(ostream &cout,const vector<pair<typC,typD>> &a) { for (auto &x:a) cout<<x<<'\n'; return cout; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
// #define _GLIBCXX_DEBUG
// clang-format on

bool f(int i, int cur, vector<int> &v, int sum, vector<vector<int>> &dp)
{
    if (i >= v.size())
        return 0;

    if (dp[i][cur] != -1)
        return dp[i][cur];
    int x = cur;
    x += v[i];
    x %= 9;
    int pick = 0, notpick = 0;
    if (x == 9 - sum)
    {
        return 1;
    }
    pick = f(i + 1, x, v, sum, dp);
    notpick = f(i + 1, cur, v, sum, dp);
    return dp[i][cur] = (pick || notpick);
}
void solve()
{
    ll t, m, n, a, b;
    string h;
    cin >> h;
    int sum = 0;
    vector<int> v;
    for (char &c : h)
    {
        int x = c - '0';
        if (x <= 3)
        {
            if (x == 2)
                v.push_back(2);
            if (x == 3)
                v.push_back(6);
        }
        sum += x;
    }
    sum %= 9;
    dbg(sum, v);
    if (!sum)
    {
        cout << "YES" << endl;
        return;
    }
    vector<vector<int>> dp(v.size() + 1, vector<int>(10, -1));
    if (f(0, 0, v, sum, dp))
    {
        cout << "YES" << endl;
    }
    else
        cout << "NO" << endl;
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Answer: 2


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

}

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

}

Answer: 1


#include <bits/stdc++.h>
#define ll long long
using namespace std;

vector<bool> digitchanges(9);
 //track the digits already searched

bool searchchanges(int curmod9, int remaining2s, int remaining3s){
    if(curmod9 == 0){
        return true;
    }
    // 1 2 3 4 5 6 7 8 0
    /*
    if(digitchanges[curmod9] == true){
        cout << curmod9 << "\n";
        return false;
    }
    */
    if(remaining2s == 0 && remaining3s == 0){
       // cout << remaining2s << " " << remaining3s << "\n";
        return false;
    }
    digitchanges[curmod9] = true;
    bool true2 = false;
    bool true3 = false;
    if(remaining2s > 0){
        int newmod9 = (2 + curmod9) % 9;
        true2 = searchchanges(newmod9, remaining2s-1, remaining3s);
    }
    if(remaining3s > 0){
        int newmod9 = (6+curmod9) % 9;
        true3 = searchchanges(newmod9, remaining2s, remaining3s-1);
    }
    if(true2 == true || true3 == true){
        return true;
    }
    return false;
}

void solve() {
    int n[2]; //only store twos and threes
    n[0] = 0; //count of 2s
    n[1] = 0; //count of 3s
    int totalmod9 = 0;
    string s;
    cin >> s;
    int len = s.length();
    for(int i = 0; i < len; i++){
        if(s[i] == '3'){
            n[1]++;
        }
        if(s[i] == '2'){
            n[0]++;
        }
        int digit = s[i] - '0';
        totalmod9 += digit;
        totalmod9 = totalmod9 % 9;
    }

    if(n[0] > 10){
        cout << "YES";
    } else if(n[0]>5&&n[1]>5){
        cout << "YES";
    }else if(searchchanges(totalmod9, n[0], n[1]) == true){
        cout << "YES";
    } else{
        cout << "NO";
    }
    return;
}

int main() {
    int TESTS; cin >> TESTS;
    while (TESTS --> 0) {
        solve();
        cout << '\n';
    }
    return 0;
}

#include <bits/stdc++.h>
#define ll long long
using namespace std;

vector<bool> digitchanges(9);
 //track the digits already searched

bool searchchanges(int curmod9, int remaining2s, int remaining3s, int iterator){
    if(curmod9 == 0){
        return true;
    }
    if(iterator > 9){
        return false;
    }
    iterator++;
    // 1 2 3 4 5 6 7 8 0
    /*
    if(digitchanges[curmod9] == true){
        cout << curmod9 << "\n";
        return false;
    }
    */
    if(remaining2s == 0 && remaining3s == 0){
       // cout << remaining2s << " " << remaining3s << "\n";
        return false;
    }
    digitchanges[curmod9] = true;
    bool true2 = false;
    bool true3 = false;
    if(remaining2s > 0){
        int newmod9 = (2 + curmod9) % 9;
        true2 = searchchanges(newmod9, remaining2s-1, remaining3s, iterator);
    }
    if(remaining3s > 0){
        int newmod9 = (6+curmod9) % 9;
        true3 = searchchanges(newmod9, remaining2s, remaining3s-1, iterator);
    }
    if(true2 == true || true3 == true){
        return true;
    }
    return false;
}

void solve() {
    int n[2]; //only store twos and threes
    n[0] = 0; //count of 2s
    n[1] = 0; //count of 3s
    int totalmod9 = 0;
    string s;
    cin >> s;
    int len = s.length();
    for(int i = 0; i < len; i++){
        if(s[i] == '3'){
            n[1]++;
        }
        if(s[i] == '2'){
            n[0]++;
        }
        int digit = s[i] - '0';
        totalmod9 += digit;
        totalmod9 = totalmod9 % 9;
    }

    if(n[0] > 9){
        cout << "YES";
    } else if(n[0]>3&&n[1]>3){
        cout << "YES";
    } else if(n[1]>3 && totalmod9%3==0){
        cout << "YES";
    }else if(searchchanges(totalmod9, n[0], n[1], 0) == true){
        cout << "YES";
    } else{
        cout << "NO";
    }
    return;
}

int main() {
    int TESTS; cin >> TESTS;
    while (TESTS --> 0) {
        solve();
        cout << '\n';
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#define inpv(v,n) for(int i=0;i<n;i++)cin>>v[i]
#define disp(v) for(auto x:v) cout<<x<<" "; cout<<"\n"
#define srt(x) sort(x.begin(),x.end())
#define N "\n"
using namespace std;
typedef long long ll;
typedef vector <ll> vll;

void solve() {
    // Your code here
    string s;
    cin>>s;
    int sum=0;
    ll three=0;
    ll two=0;
    for(ll i=0;i<s.length();i++)
    {
        sum+=s[i]-'0';
        switch(s[i])
        {
            case '2':two++;
            break;
            case '3':three++;
            break;
        }
    }
    sum=sum%9;
    bool yes=false;
    two=min(9LL,two);
    three=min(three,9LL);
    for(ll i=0;i<=two;i++)
    {
        for(ll j=0;j<=three;j++)
        {
            if((i*2+j*6+sum)%9==0)
            {
                yes=true;
                break;
            }
        }
    }
    if(sum==0)yes=true;
    //cout<<two<<" "<<three<<N;
    cout<<(yes?"YES\n":"NO\n");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll t;
    cin >> t;
    while (t--) solve();
}

#include <bits/stdc++.h>
#define inpv(v,n) for(int i=0;i<n;i++)cin>>v[i]
#define disp(v) for(auto x:v) cout<<x<<" "; cout<<"\n"
#define srt(x) sort(x.begin(),x.end())
#define N "\n"
using namespace std;
typedef long long ll;
typedef vector <ll> vll;

void solve() {
    // Your code here
    string s;
    cin>>s;
    int sum=0;
    ll three=0;
    ll two=0;
    for(ll i=0;i<s.length();i++)
    {
        sum+=s[i]-'0';
        switch(s[i])
        {
            case '2':two++;
            break;
            case '3':three++;
            break;
        }
    }
    sum=sum%9;
    bool yes=false;
    two=two%9;
    three=three%9;
    for(ll i=0;i<=two;i++)
    {
        for(ll j=0;j<=three;j++)
        {
            if((i*2+j*6+sum)%9==0)
            {
                yes=true;
                break;
            }
        }
    }
    if(sum==0)yes=true;
    //cout<<two<<" "<<three<<N;
    cout<<(yes?"YES\n":"NO\n");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll t;
    cin >> t;
    while (t--) solve();
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;

#define RANGE(i, a, b)                                                         \
    for (auto i = a - (a > b); (i < b) ^ (a > b); i += 1 - 2 * (a > b))
#define ll long long
/*
 1 -> 1 No change
 2 -> 4: +2 // only this can be applied twice, in that case: +5
 3 -> 9: +6
 So we can change x 2s to 4, and y 3s to 6s. The new remainder of the number
 becomes 2*x + 6*y == 0 [9]. We must find a solution for (x, y), with the upper
 bound being the actual number of occurence of 2s and 3s.
 Les restes possible d'un multiplication par 2 mod 9:
 1*2 = 2 [9]
 2*2 = 4 [9]
 3*2 = 6 [9]
 4*2 = 8 [9]
 5*2 = 0 [9]
 Les restes possible d'une multiplication par 6 mod 9:
 1*6 = 6 [9]
 2*6 = 3 [9]
 3*6 = 0 [9]
 Tableau des additions possibles:
 ---- 0 2 4 6 8
    0 0 2 4 6 7
    3 3 5 7 0 2
    6 6 8 1 3 5

*/

void solve() {
    string n;
    cin >> n;
    ll count2 = 0, count3 = 0;
    ll rem = 0;
    for (auto d : n) {
        int c = d - '0';
        rem += c % 10;
        rem %= 9;
        if (c % 10 == 2)
            count2++;
        else if (c % 10 == 3)
            count3++;
    }
    for (int n2 = 0; n2 <= min(count2, 10ll); n2++)
        for (int n3 = 0; n3 <= min(count3, 10ll); n3++)
            if (((n2 * 2) % 9 + (n3 * 6) % 9 + rem) % 9 == 0) {
                cout << "Yes" << endl;
                return;
            }
    cout << "No" << endl;
}

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

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define RANGE(i, a, b)                                                         \
    for (auto i = a - (a > b); (i < b) ^ (a > b); i += 1 - 2 * (a > b))
#define ll long long
/*
 1 -> 1 No change
 2 -> 4: +2 // only this can be applied twice, in that case: +5
 3 -> 9: +6
 So we can change x 2s to 4, and y 3s to 6s. The new remainder of the number
 becomes 2*x + 6*y == 0 [9]. We must find a solution for (x, y), with the upper
 bound being the actual number of occurence of 2s and 3s.
 Les restes possible d'un multiplication par 2 mod 9:
 1*2 = 2 [9]
 2*2 = 4 [9]
 3*2 = 6 [9]
 4*2 = 8 [9]
 5*2 = 0 [9]
 Les restes possible d'une multiplication par 6 mod 9:
 1*6 = 6 [9]
 2*6 = 3 [9]
 3*6 = 0 [9]
 Tableau des additions possibles:
 ---- 0 2 4 6 8
    0 0 2 4 6 7
    3 3 5 7 0 2
    6 6 8 1 3 5

*/

void solve() {
    ll n;
    cin >> n;
    ll count2 = 0, count3 = 0;
    ll rem = 0;
    for (; n > 0; n /= 10) {
        rem += n % 10;
        if (n % 10 == 2)
            count2++;
        else if (n % 10 == 3)
            count3++;
    }
    rem %= 9;
    // cout << count2 << " " << count3 << " " << rem << endl;
    // if (count2 >= 5 && count3 >= 3) {
    //     cout << "Yes" << endl;
    //     return;
    // }
    for (int n2 = 0; n2 <= min(count2, 10ll); n2++)
        for (int n3 = 0; n3 <= min(count3, 10ll); n3++)
            if (((n2 * 2) % 9 + (n3 * 6) % 9 + rem) % 9 == 0) {
                cout << "Yes" << endl;
                return;
            }
    cout << "No" << endl;
}

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

    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 1


#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <algorithm>

using namespace std;

int main(){
    int tc;
    cin>>tc;
    for(int _=0; _<tc; _++){
        string x;
        cin>>x;
        long long sum=0;
        int twos=0;
        int threes=0;
        for(long long i=0; i<x.length(); i++){
            int n = x[i]-48;
            sum+=n;
            if(n==2) twos++;
            if(n==3) threes++;
        }

        bool flag=false;
        if(sum%9==0 || sum==0){
            flag=true;
        } else {
            for(long long twth=1, tw=1, th=1; twth<=min(twos,threes) || tw<=twos || th<=threes; tw++, th++, twth++){
                if(tw<=twos && (sum+2*tw)%9==0){
                    // cout<<"twos: "<<tw<<endl;
                    flag=true;
                    break;
                }
                if(th<=threes && (sum+6*th)%9==0){
                    // cout<<"threes: "<<th<<endl;
                    flag=true;
                    break;
                }
                if((sum+8*twth)%9==0){
                    // cout<<"twos and threes: "<<twth<<endl;
                    flag=true;
                    break;
                }
            }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
    return 0;
}

#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#include <algorithm>

using namespace std;

int main(){
    int tc;
    cin>>tc;
    for(int _=0; _<tc; _++){
        string x;
        cin>>x;
        long long sum=0;
        int twos=0;
        int threes=0;
        for(long long i=0; i<x.length(); i++){
            int n = x[i]-48;
            sum+=n;
            if(n==2) twos++;
            if(n==3) threes++;
        }

        bool flag=false;
        if(sum%9==0 || sum==0){
            flag=true;
        } else {
            for(long long tw=0; tw<=twos; tw++){
                for(long long th=0; th<=threes; th++){
                    if((sum+2*tw+6*th)%9==0){
                        // cout<<"twos: "<<tw<<endl;
                        flag=true;
                        break;
                    }
                }
            }
            // if(!flag){
            //     for(long long th=0; th<=threes; th++){
            //         if((sum+6*th)%9==0){
            //             // cout<<"threes: "<<th<<endl;
            //             flag=true;
            //             break;
            //         }
            //     }
            // }
            // if(!flag){
            //     for(long long twth=1; twth<=min(twos,threes);twth++){
            //         if((sum+8*twth)%9==0){
            //             // cout<<"twos and threes: "<<twth<<endl;
            //             flag=true;
            //             break;
            //         }
            //     }
            // }
        }
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;

    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define read(arr) for (auto &x : arr) cin >> x;
#define print(arr) for (auto &x : arr) cout << x << ' '; cout << endl;
#define sortv(arr) sort(arr.begin(), arr.end())
#define sorta(arr) sort(arr.begin(), arr.end())
#define revers(arr) reverse(arr.begin(), arr.end())
#define ll long long
#define vi vector<int>
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL))

void solve() {
    ll n;
    cin>>n;
    ll c2=0,c3=0;
    ll sum=0;
    while(n){
        int l =n%10;
        if(l==2){
            c2++;
        }
        if(l==3){
            c3++;
        }
        sum+=l;
        n/=10;
    }
    if(c2>10) c2=10;
    if(c3>10) c3=10;
    for(int i =0;i<=c2;i++){
       for(int j =0;j<=c3;j++){
        ll x =sum + i*2 + j*6;
        if(x%9==0){
            cout<<"YES"<<endl;
            return;
        }
       }
    }
    cout<<"NO"<<endl;
    return ;
}

int main() {
    fastread(); // Enable fast input/output
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

#include <bits/stdc++.h>
using namespace std;

#define endl "\n"
#define read(arr) for (auto &x : arr) cin >> x;
#define print(arr) for (auto &x : arr) cout << x << ' '; cout << endl;
#define sortv(arr) sort(arr.begin(), arr.end())
#define sorta(arr) sort(arr.begin(), arr.end())
#define revers(arr) reverse(arr.begin(), arr.end())
#define ll long long
#define vi vector<int>
#define fastread() (ios_base::sync_with_stdio(false), cin.tie(NULL))

void solve() {
    string s;
    cin>>s;
    ll c2=0,c3=0;
    ll sum=0;
    for(auto &i:s){
        if(i=='2') c2++;
        if(i=='3') c3++;
        sum+=i-'0';
    }
    if(c2>10) c2=10;
    if(c3>10) c3=10;
    for(int i =0;i<=c2;i++){
       for(int j =0;j<=c3;j++){
        ll x =sum + i*2 + j*6;
        if(x%9==0){
            cout<<"YES"<<endl;
            return;
        }
       }
    }
    cout<<"NO"<<endl;
    return ;
}

int main() {
    fastread(); // Enable fast input/output
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define all(x) x.begin(),x.end() ;
using namespace std;
int main(){

   ios_base::sync_with_stdio(false);
   cin.tie(NULL);cout.tie(NULL);
    int testcase;
    cin>>testcase;
    while(testcase--){
        string s;
        cin>>s;
        int cnttwo=0,cntthree=0 ,sum=0;
        for(int i=0;i<s.size();i++){
            sum+=(s[i]-'0');
            if(s[i]=='2')cnttwo++;
            if(s[i]=='3')cntthree++;
        }
        if(sum%9==0)cout<<"Yes\n";

        else{
        vector<int>two;
        vector<int>six;
        vector<int>eight;
        int sum_=sum,sum__=sum,sum___=sum;
        for(int i=0;i<cnttwo;i++){
                sum_+=2;
                two.push_back(sum_);
        }
        for(int i=0;i<cntthree;i++){
            sum__+=6;
            sum_+=6;
            six.push_back(sum__);
            two.push_back(sum_);
        }
        for(int i=0;i<cnttwo;i++){
                sum__+=2;
                six.push_back(sum__);
        }
        for(int i=0;i<min(cnttwo,cntthree);i++){
            sum___+=8;
            eight.push_back(sum___);
        }
        set<int>st;
        for(auto it: two){
            st.insert(it);
        }
       for(auto it: six)st.insert(it);
        for(auto it: eight)st.insert(it);

            bool flag=false;
       for(auto it: st){
        if(it%9==0){
//            cout<<"Yes\n";
            flag=true;
        }
       }
       flag?cout<<"Yes\n":cout<<"No\n";
      }

    }
    return 0;
}

Answer: 1


#include <bits/stdc++.h>

using namespace std;

int main() {

    int t; cin >> t;

    while (t--) {

        long long n; cin >> n;

        if(n%9 == 0){

            cout << "yes" << endl;

            continue;

        }

        long long sum = 0;

        int cnt2 = 0, cnt3 = 0;

        long long temp = n;

        while(temp>0){

            int d = temp%10;

            sum += d;

            if(d==2) cnt2++;

            if(d==3) cnt3++;

            temp /= 10;

        }

        int r = 9-sum % 9;

        bool possible = false;

        int max_a = min(cnt2, 9);

        int max_b = min(cnt3, 3);

        for (int a = 0; a <= max_a; a++){

            for (int b = 0; b <= max_b; b++){

                if ((2 * a + 6 * b) % 9 == r) {

                    possible = true;

                    break;

                }

            }

            if(possible) break;

        }

        cout << (possible ? "yes" : "no") << endl;

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

int main() {

    int t; cin >> t;

    while (t--) {

        string n; cin >> n;

        long long sum = 0;

        int cnt2 = 0, cnt3 = 0;

        for (char c : n) {

            int d = c - '0';

            sum += d;

            if(d == 2) cnt2++;

            if(d == 3) cnt3++;

        }

        if(sum % 9 == 0) {

            cout << "yes" << endl;

            continue;

        }

        int r = 9-(sum % 9);

        bool possible = false;

        int max_a = min(cnt2, 9);

        int max_b = min(cnt3, 3);

        for (int a = 0; a <= max_a; a++){

            for (int b = 0; b <= max_b; b++){

                if ((2*a + 6*b) % 9==r) {

                    possible = true;

                    break;

                }

            }

            if(possible) break;

        }

        cout << (possible ? "yes" : "no") << endl;

    }

    return 0;

}

Answer: 2


#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <algorithm>

#include <numeric>

using namespace std;

using ll = long long;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);

#define f(i, a, b) for (int i = a; i < b; ++i)

#define pb push_back

/*vector<int> getDigits(ll num) {

    vector<int> digits;

    while (num > 0) {

        digits.push_back(num % 10);

        num /= 10;

    }

    return digits;

}*/

void solve() {

    string n;

    cin >> n;

    //vector<int> num;

    //num= getDigits(n);

    //int k = accumulate(num.begin(), num.end(), 0LL);

    int k=0;

    for(char i:n){

        k=k+(i-'0');

    }

    bool no= true;

    int tw=0;

    int si=0;

    for(char i:n){

        if(i=='2') tw++;

        if(i=='3') si++;

    }

    tw=min(tw,10);

    si=min(si,10);

    f(i,0,tw+1){

        f(j,0,si+1){

            if((k+2*i+6*j)%9==0){

                cout<<"Yes"<<"\n";

                no=false;

                break;

            }

        }

    }

    if(no) cout<<"No"<<"\n";

}

int main() {

    fastio;

    int t;

    cin >> t;

    while (t--) solve();

    return 0;

}

#include <iostream>

#include <string>

#include <vector>

#include <set>

#include <algorithm>

#include <numeric>

using namespace std;

using ll = long long;

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);

#define f(i, a, b) for (int i = a; i < b; ++i)

#define pb push_back

/*vector<int> getDigits(ll num) {

    vector<int> digits;

    while (num > 0) {

        digits.push_back(num % 10);

        num /= 10;

    }

    return digits;

}*/

void solve() {

    string n;

    cin >> n;

    //vector<int> num;

    //num= getDigits(n);

    //int k = accumulate(num.begin(), num.end(), 0LL);

    int k=0;

    for(char i:n){

        k=k+(i-'0');

    }

    int tw=0;

    int si=0;

    for(char i:n){

        if(i=='2') tw++;

        if(i=='3') si++;

    }

    tw=min(tw,10);

    si=min(si,10);

    f(i,0,tw+1){

        f(j,0,si+1){

            int v=(k+2*i+6*j);

            if(v%9==0){

                cout<<"Yes"<<"\n";

                return;

            }

        }

    }

    cout<<"No"<<"\n";

}

int main() {

    fastio;

    int t;

    cin >> t;

    while (t--) solve();

    return 0;

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

void solve() {
    string s;
    cin >> s;
    map<char,int> mp;
    mp['2']=0;
    mp['3']=0;
    ll sum=0;
    for(int i=0;i<s.size();i++){
        if(s[i]=='2') mp['2']++;
        if(s[i]=='3') mp['3']++;
        sum += (s[i]-'0');
        sum %= 9;
    }
     for (int i = 0; i <= min(10, mp['2']); i++) {
        for (int j = 0; j <= min(10, mp['3']); j++) {
            if ((sum + i * 2 + j * 6) % 9 == 0) {
                cout << "YES" << endl;
                return;
            }
        }
    }

    cout << "NO" << endl;
}

int main() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}

Answer: 2


/*
 *
 *@author - pred695
 *
 */
/*弱い も
خاک سے آیا ہوں، خاک بنوں گا۔*/
/*-------------------------------------------****************-------------------------------------------------------****************----------------------------------------------------------------------*/
#include <bits/stdc++.h>
using namespace std;
#define vi                          vector<int>
#define vvi                         vector<vector<int>>
#define pb                          push_back
#define eb                          emplace_back
#define pii                         pair<int, int>
#define vp                          vector<pii>
#define ppb                         pop_back
#define pf                          push_front
#define ppf                         pop_front
#define int                         long long
#define ull                         unsigned long long
#define lld                         long double
#define all(x)                      x.begin(),x.end()
#define rep(i, a, b)                for(int i = a; i < b; i++)
#define repr(i, a, b)               for(int i = a; i >= b; i--)
#define read(a, n)                  rep(i, 0, n)    cin >> a[i]
#define readx(a, n, m)              rep(i, 0, n){rep(j, 0, m) cin >> a[i][j];}
#define print(a, n)                 rep(i, 0, n)    cout << a[i] << " "; cout << endl
#define printx(a, n, m)             rep(i, 0, n){rep(j, 0, m) cout << a[i][j] << " "; cout << endl;}
#define endl                        "\n"
#define ff                          first
#define ss                          second
#define yes                         cout << "YES" << endl
#define no                          cout << "NO" << endl
#define M                           1000000007
#define prec(x)                     cout << fixed << setprecision(x)
#define bpc(x)                      __builtin_popcountll(x)
#define sz(x)                       (int)(x.size())
#define bpl(x)                      __builtin_clzll(x)
#define bpt(x)                      __builtin_ctzll(x)
#define lg(x)                      __lg(x)

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<": "; deb(x); cerr << endl;
#else
#define debug(x)
#endif

void deb(int t) {cerr << t;}
void deb(string t) {cerr << t;}
void deb(char t) {cerr << t;}
void deb(double t) {cerr << t;}
void deb(ull t) {cerr << t;}

template <class T, class V> void deb(pair <T, V> p) {cerr << "{"; deb(p.ff); cerr << ","; deb(p.ss); cerr << "}";}
template <class T> void deb(vector <T> v) {cerr << "[ "; for (T i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T> void deb(set <T> v) {cerr << "[ "; for (T i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T> void deb(multiset <T> v) {cerr << "[ "; for (T i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T, class V> void deb(map <T, V> v) {cerr << "[ "; for (auto i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T, class V> void deb(map<T, vector<V>> v) {cerr << "{ "; for (auto i : v) {cerr  << i.first << ": ";deb(i.second);cerr << " ";}cerr << "}";}
template <class T> void deb(priority_queue<T> pq) {cerr << "[ "; while (!pq.empty()) {deb(pq.top()); cerr << " "; pq.pop();} cerr << "]";}
template <class T> void deb(deque<T> dq) {cerr << "[ "; for (T i : dq) {deb(i); cerr << " ";} cerr << "]";}
template <class T> void deb(list<T> lst) {cerr << "[ "; for (T i : lst) {deb(i); cerr << " ";} cerr << "]";}
template <class T, class V> void deb(unordered_map<T, V> um) {cerr << "{ "; for (auto i : um) {cerr << i.first << ": "; deb(i.second); cerr << " ";} cerr << "}";}

void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int ipow(int a, int b){
   int ans = 1;
   while(b)
   {
    if(b & 1)
    ans = (ans*a)%M;
    a = (a*a)%M;
    b>>=1;
   }
   return ans;
}
clock_t start;
void exectime(){
    clock_t end=clock();
    double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
    #ifndef ONLINE_JUDGE
    cout << endl;
    cout << endl;
    cout << "Executed in "<< fixed << time_taken << setprecision(10);
    cout << " secs " << endl;
    #endif
}
const int d4x[4]={-1, 0, 1, 0}, d4y[4]={0, 1, 0, -1};
const int d8x[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8]={0, 1, 1, 1, 0, -1, -1, -1};
const int c8x[8]={-2, -1, 1, 2, 2, 1, -1, -2}, c8y[8]={1, 2, 2, 1, -1, -2, -2, -1};
vector<int> custom_c8x(8, 0), custom_c8y(8, 0);
void make_custom_movements(int a, int b){
    custom_c8x={b, -b, b, -b, a, -a, a, -a};
    custom_c8y={a, a, -a, -a, b, b, -b, -b};
}

struct custom_priority{
    bool operator() (pii &a, pii &b){
        if(a.ff == b.ff){
            return a.ss < b.ss;
        }
      return a.ff > b.ff;
    }
};

const int N = 1e7 + 10;

/*----------------------------------------------------------------------MAIN CODE------------------------------------------------------------------------------------------------------------------------*/
void solve(){
    string n;
    cin >> n;
    ull s = 0;
    int two = 0, three = 0;
    for(auto &x : n) {
        two += x == '2';
        three += x == '3';
        s += (x - '0');
    }
    // only two and three needs to be considered.
    for(int i = 0; i <= min(two, 8LL); i++) {
        for(int j = 0; j <= min(three, 8LL); j++) {
            debug(s + 6 * j)
            if((s + (i * 2) + (j * 6)) % 9 == 0) {
                yes;
                return;
            }
        }
    }
    no;

}
/*----------------------------------------------------------------------ありがと-------------------------------------------------------------------------------------------------------------------------*/
signed main(){
fast();
int t = 1;
cin >> t;
start = clock();
for(int i = 0; i < t; i++){
    // cout << "Case #" << i + 1 << ": ";
    solve();
}
exectime();
return 0;
}

/*
 *
 *@author - pred695
 *
 */
/*弱い も
خاک سے آیا ہوں، خاک بنوں گا۔*/
/*-------------------------------------------****************-------------------------------------------------------****************----------------------------------------------------------------------*/
#include <bits/stdc++.h>
using namespace std;
#define vi                          vector<int>
#define vvi                         vector<vector<int>>
#define pb                          push_back
#define eb                          emplace_back
#define pii                         pair<int, int>
#define vp                          vector<pii>
#define ppb                         pop_back
#define pf                          push_front
#define ppf                         pop_front
#define int                         long long
#define ull                         unsigned long long
#define lld                         long double
#define all(x)                      x.begin(),x.end()
#define rep(i, a, b)                for(int i = a; i < b; i++)
#define repr(i, a, b)               for(int i = a; i >= b; i--)
#define read(a, n)                  rep(i, 0, n)    cin >> a[i]
#define readx(a, n, m)              rep(i, 0, n){rep(j, 0, m) cin >> a[i][j];}
#define print(a, n)                 rep(i, 0, n)    cout << a[i] << " "; cout << endl
#define printx(a, n, m)             rep(i, 0, n){rep(j, 0, m) cout << a[i][j] << " "; cout << endl;}
#define endl                        "\n"
#define ff                          first
#define ss                          second
#define yes                         cout << "YES" << endl
#define no                          cout << "NO" << endl
#define M                           1000000007
#define prec(x)                     cout << fixed << setprecision(x)
#define bpc(x)                      __builtin_popcountll(x)
#define sz(x)                       (int)(x.size())
#define bpl(x)                      __builtin_clzll(x)
#define bpt(x)                      __builtin_ctzll(x)
#define lg(x)                      __lg(x)

#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<": "; deb(x); cerr << endl;
#else
#define debug(x)
#endif

void deb(int t) {cerr << t;}
void deb(string t) {cerr << t;}
void deb(char t) {cerr << t;}
void deb(double t) {cerr << t;}
void deb(ull t) {cerr << t;}

template <class T, class V> void deb(pair <T, V> p) {cerr << "{"; deb(p.ff); cerr << ","; deb(p.ss); cerr << "}";}
template <class T> void deb(vector <T> v) {cerr << "[ "; for (T i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T> void deb(set <T> v) {cerr << "[ "; for (T i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T> void deb(multiset <T> v) {cerr << "[ "; for (T i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T, class V> void deb(map <T, V> v) {cerr << "[ "; for (auto i : v) {deb(i); cerr << " ";} cerr << "]";}
template <class T, class V> void deb(map<T, vector<V>> v) {cerr << "{ "; for (auto i : v) {cerr  << i.first << ": ";deb(i.second);cerr << " ";}cerr << "}";}
template <class T> void deb(priority_queue<T> pq) {cerr << "[ "; while (!pq.empty()) {deb(pq.top()); cerr << " "; pq.pop();} cerr << "]";}
template <class T> void deb(deque<T> dq) {cerr << "[ "; for (T i : dq) {deb(i); cerr << " ";} cerr << "]";}
template <class T> void deb(list<T> lst) {cerr << "[ "; for (T i : lst) {deb(i); cerr << " ";} cerr << "]";}
template <class T, class V> void deb(unordered_map<T, V> um) {cerr << "{ "; for (auto i : um) {cerr << i.first << ": "; deb(i.second); cerr << " ";} cerr << "}";}

void fast(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
int ipow(int a, int b){
   int ans = 1;
   while(b)
   {
    if(b & 1)
    ans = (ans*a)%M;
    a = (a*a)%M;
    b>>=1;
   }
   return ans;
}
clock_t start;
void exectime(){
    clock_t end=clock();
    double time_taken = double(end - start) / double(CLOCKS_PER_SEC);
    #ifndef ONLINE_JUDGE
    cout << endl;
    cout << endl;
    cout << "Executed in "<< fixed << time_taken << setprecision(10);
    cout << " secs " << endl;
    #endif
}
const int d4x[4]={-1, 0, 1, 0}, d4y[4]={0, 1, 0, -1};
const int d8x[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8y[8]={0, 1, 1, 1, 0, -1, -1, -1};
const int c8x[8]={-2, -1, 1, 2, 2, 1, -1, -2}, c8y[8]={1, 2, 2, 1, -1, -2, -2, -1};
vector<int> custom_c8x(8, 0), custom_c8y(8, 0);
void make_custom_movements(int a, int b){
    custom_c8x={b, -b, b, -b, a, -a, a, -a};
    custom_c8y={a, a, -a, -a, b, b, -b, -b};
}

struct custom_priority{
    bool operator() (pii &a, pii &b){
        if(a.ff == b.ff){
            return a.ss < b.ss;
        }
      return a.ff > b.ff;
    }
};

const int N = 1e7 + 10;

/*----------------------------------------------------------------------MAIN CODE------------------------------------------------------------------------------------------------------------------------*/
void solve(){
    int n;
    cin >> n;
    int s = 0;
    unordered_map<int, int> mp;
    mp[2] = 0;
    mp[3] = 0;
    while(n > 0) {
        s += n % 10;
        mp[n % 10]++;
        n /= 10;
    }
    // only two and three needs to be considered.
    if(s % 9 == 0) {
        yes;
        return;
    }
    int temp = s;
    debug(s)
    debug(mp[2])
    debug(mp[3])
    for(int i = 0; i <= min(mp[2], 8LL); i++) {
        s += i * 2;
        for(int j = 0; j <= min(mp[3], 8LL); j++) {
            debug(j)
            debug(i)
            debug(s + 6 * j)
            if((s + (j * 6)) % 9 == 0) {
                yes;
                return;
            }
        }
        s = temp;
    }
    no;

}
/*----------------------------------------------------------------------ありがと-------------------------------------------------------------------------------------------------------------------------*/
signed main(){
fast();
int t = 1;
cin >> t;
start = clock();
for(int i = 0; i < t; i++){
    // cout << "Case #" << i + 1 << ": ";
    solve();
}
exectime();
return 0;
}

Answer: 1


Answer: 2


using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define si set<int>
#define sll set<long long>
#define sc set<char>
#define ss set<string>
#define usi unordered_set<int>
#define pqi priority_queue<int>
#define pqll priority_queue<ll>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pii pair<int, int>
#define pllll pair<long long, long long>
#define f(i,s,e) for(int i = s; i < e; i++)
#define cf(i,s,e) for(int i = s; i <= e; i++)
#define rf(i,e,s) for(int i = e-1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define eb embrace_back
#define mp make_pair
#define fi first
#define se second
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)(x).size()
#define fastioa ios::sync_with_stdio(0); cin.tie(0); freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);
#define fastio ios::sync_with_stdio(0); cin.tie(0);
#define MOD 1000000007
#define inf 1e9  // Use for int (e.g., distance)
#define inf_ll 1e18  // Use for long long
#define eps 1e-9  // Precision for floating-point comparisons

void solve()
{
    string number;
    cin >> number;
    ll sum = 0;
    int a = 0, b = 0;
    for (char digit : number)
    {
        sum += (digit - '0');
        if (digit - '0' == 2) a++;
        if (digit - '0' == 3) b++;
    }
    f (i, 0, min(8, a + 1))
    {
        f (j, 0, min(8, b + 1))
        {
            if ((sum + i * 2 + j * 6) % 9 == 0)
            {
                cout << "YES" << "\n"; return;
            }
        }
    }
    cout << "No" << "\n";

}
int main()
{
    fastio
    int t; cin >> t;
    while (t --)
    {
        solve();
    }
}

using namespace std;
#include <bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<long long>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define si set<int>
#define sll set<long long>
#define sc set<char>
#define ss set<string>
#define usi unordered_set<int>
#define pqi priority_queue<int>
#define pqll priority_queue<ll>
#define mii map<int, int>
#define umii unordered_map<int, int>
#define pii pair<int, int>
#define pllll pair<long long, long long>
#define f(i,s,e) for(int i = s; i < e; i++)
#define cf(i,s,e) for(int i = s; i <= e; i++)
#define rf(i,e,s) for(int i = e-1; i >= s; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define pb push_back
#define eb embrace_back
#define mp make_pair
#define fi first
#define se second
#define ub upper_bound
#define lb lower_bound
#define sz(x) (int)(x).size()
#define fastioa ios::sync_with_stdio(0); cin.tie(0); freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);
#define fastio ios::sync_with_stdio(0); cin.tie(0);
#define MOD 1000000007
#define inf 1e9  // Use for int (e.g., distance)
#define inf_ll 1e18  // Use for long long
#define eps 1e-9  // Precision for floating-point comparisons

void solve()
{
    string number;
    cin >> number;
    ll sum = 0;
    int a = 0, b = 0;
    for (char digit : number)
    {
        sum += (digit - '0');
        if (digit - '0' == 2) a++;
        if (digit - '0' == 3) b++;
    }
    cf (i, 0, min(8, a))
    {
        cf (j, 0, min(8, b))
        {
            if ((sum + i * 2 + j * 6) % 9 == 0)
            {
                cout << "YES" << "\n"; return;
            }
        }
    }
    cout << "No" << "\n";

}
int main()
{
    fastio
    int t; cin >> t;
    while (t --)
    {
        solve();
    }
}

Answer: 2


#include <bits/stdc++.h>
    #include <ctime>
    #include <chrono>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    #define cy() cout << "YES" << '\n'
    #define cn() cout << "NO" << '\n'
    const double pi=    3.14159265358979323846;

    // Function to check if an integer is prime or not
    bool isprime(ll n){
        if(n<2){
            return false;
        }
        for(ll i = 2;i<=sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    bool pointscompare(pair<string,int> &a ,pair<string,int>  &b){
        return a.second>b.second;
    }
    bool goaldiffcompare(pair<string,int> &a , pair<string,int> &b){
        return a.second>b.second;
    }
    bool cmp(pair<string, int>& a,
        pair<string, int>& b)
    {
        return a.second < b.second;
    }
    bool isSorted(const std::vector<int>& arr) {
    for (size_t i = 1; i < arr.size(); ++i) {
        if (arr[i] < arr[i - 1]) {
            return false;
        }
        }
        return true;
    }

    void solve(){
        //     ...............................
        //     >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        string s;
        cin >> s;
        int sum = 0;
        int two = 0;
        int three = 0;

        for(int i = 0;i<s.size();i++){
            sum += s[i] - '0';
            if(s[i]=='2'){
                two+=1;
            }
            if(s[i]=='3'){
                three+=1;
            }
        }
        map<pair<int,int>,int> v;
        for(int i = 0;i<=two;i++){
            for(int j = 0;j<=three;j++){
                v[{i,j}] = 2*i + 6*j;
            }
        }
        for(auto x : v){
            // cout << x.first.first << " " << x.first.second << " " << x.second << ' ' << sum%9 << '\n';
        }
        bool p =  false;
        for(auto x : v){
            if((sum+x.second)%9==0){
                cout << "YES" << '\n';
                p = true;
                break;
            }
        }
        if(!p){
            cout << "NO" << '\n';
        }

    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        std::cout.tie(0);
        // freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
        int tt;
        cin >> tt;
        while(tt--){
            using std::chrono::high_resolution_clock;
            using std::chrono::duration_cast;
            using std::chrono::duration;
            using std::chrono::milliseconds;
            auto t1 = high_resolution_clock::now();
           solve();
            auto t2 = high_resolution_clock::now();
            auto ms_int = duration_cast<milliseconds>(t2 - t1);
            duration<double, std::milli> ms_double = t2 - t1;
            // std::cout << ms_double.count() << "ms\n";
        }
        return 0;
    }

#include <bits/stdc++.h>
    #include <ctime>
    #include <chrono>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    typedef vector<int> vi;
    typedef vector<ll> vll;
    #define cy() cout << "YES" << '\n'
    #define cn() cout << "NO" << '\n'
    const double pi=    3.14159265358979323846;

    // Function to check if an integer is prime or not
    bool isprime(ll n){
        if(n<2){
            return false;
        }
        for(ll i = 2;i<=sqrt(n);i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    bool pointscompare(pair<string,int> &a ,pair<string,int>  &b){
        return a.second>b.second;
    }
    bool goaldiffcompare(pair<string,int> &a , pair<string,int> &b){
        return a.second>b.second;
    }
    bool cmp(pair<string, int>& a,
        pair<string, int>& b)
    {
        return a.second < b.second;
    }
    bool isSorted(const std::vector<int>& arr) {
    for (size_t i = 1; i < arr.size(); ++i) {
        if (arr[i] < arr[i - 1]) {
            return false;
        }
        }
        return true;
    }

    void solve(){
        //     ...............................
        //     >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        string s;
        cin >> s;
        int sum = 0;
        int two = 0;
        int three = 0;

        for(int i = 0;i<s.size();i++){
            sum += s[i] - '0';
            if(s[i]=='2'){
                two+=1;
            }
            if(s[i]=='3'){
                three+=1;
            }
        }
        set<int> v;
        if(sum%9!=0){

        for(int i = 0;i<=min(8,two);i++){
            for(int j = 0;j<=min(8,three);j++){
                v.insert(2*i + 6*j);
            }
        }
        for(auto x : v){
            // cout << x.first.first << " " << x.first.second << " " << x.second << ' ' << sum%9 << '\n';
        }
        bool p =  false;
        for(auto x : v){
            if((sum+x)%9==0){
                cout << "YES" << '\n';
                p = true;
                break;
            }
        }
        if(!p){
            cout << "NO" << '\n';
        }
        }else{
            cout << "YES" << '\n';
        }
    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        std::cout.tie(0);
        // freopen("input.txt","r",stdin);
        // freopen("output.txt","w",stdout);
        int tt;
        cin >> tt;
        while(tt--){
            using std::chrono::high_resolution_clock;
            using std::chrono::duration_cast;
            using std::chrono::duration;
            using std::chrono::milliseconds;
            auto t1 = high_resolution_clock::now();
           solve();
            auto t2 = high_resolution_clock::now();
            auto ms_int = duration_cast<milliseconds>(t2 - t1);
            duration<double, std::milli> ms_double = t2 - t1;
            // std::cout << ms_double.count() << "ms\n";
        }
        return 0;
    }

Answer: 2


#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set ;

#define int long long
#define fr(i , j, k) for(int i=j ; i<k ; i++)
#define show(v) for(auto i: v) cout << i << " " ; cout << nl;
#define take(n) int n;cin >> n;
#define pb push_back
#define nl "\n"

int mod = 1e9 + 7 ;
int power(int x , int y , int m){
    int res =1 ;
    x = x%m;
    if ( x == 0 )return 0 ;
    while( y >0 ) {
        if( y%2 )res = (res*x)%m ;
        y = y>>1 ;
        x = (x*x ) %m ;
    }
    return res ;
}
int submod( int x , int y , int mod ){
    return ( (x-y)%mod + mod )%mod;
}

int32_t main () {
    int tt;
    cin >> tt ;
    while(tt--){
        string s;
        cin >> s;
        int x = 0 , y =0 ;
        int sum =0 ;
        for(auto it: s ){
            if( it == '2')x++;
            else if ( it == '3')y++;
            sum += it-'0';
        }

        // cerr << x << " " << y <<  " " << sum << nl;
        int ans =0 ;
        fr(i,0,min((int)10,x+1)){
            fr(j,0,min((int)10,y+1)){
                int temp = (j*6 + i*2);
                // cout << temp+sum << nl;
                if( (temp + sum)%9 == 0 )ans = 1 ;
            }
        }
        if(ans )cout << "YES" << nl;
        else cout << "NO" << nl;

    }
    return 0;
}

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set ;

#define int long long
#define fr(i , j, k) for(int i=j ; i<k ; i++)
#define show(v) for(auto i: v) cout << i << " " ; cout << nl;
#define take(n) int n;cin >> n;
#define pb push_back
#define nl "\n"

int mod = 1e9 + 7 ;
int power(int x , int y , int m){
    int res =1 ;
    x = x%m;
    if ( x == 0 )return 0 ;
    while( y >0 ) {
        if( y%2 )res = (res*x)%m ;
        y = y>>1 ;
        x = (x*x ) %m ;
    }
    return res ;
}
int submod( int x , int y , int mod ){
    return ( (x-y)%mod + mod )%mod;
}

int32_t main () {
    int tt;
    cin >> tt ;
    while(tt--){
        string s;
        cin >> s;
        int x = 0 , y =0 ;
        int sum =0 ;
        for(auto it: s ){
            if( it == '2')x++;
            else if ( it == '3')y++;
            sum += it-'0';
        }
        x = x%9;
        y = y%3;
        // cerr << x << " " << y <<  " " << sum << nl;
        int ans =0 ;
        fr(i,0,x+1){
            fr(j,0,y+1){
                int temp = (j*6 + i*2);
                // cout << temp+sum << nl;
                if( (temp + sum)%9 == 0 )ans = 1 ;
            }
        }
        if(ans )cout << "YES" << nl;
        else cout << "NO" << nl;

    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
#include<algorithm>
using namespace std;
#define int long long
signed main()
{
    int tt;cin>>tt;
    while(tt--){
        int n;cin>>n;
        int sum=0;
        int ct2=0,ct3=0;
        while(n>0){
            sum+=n%10;
            if(n%10==2) ct2+=1;
            else if(n%10==3) ct3+=1;
            n/=10;
        }
        int temp=sum;
        bool f=false;
        int p=10;
        for(int i=0;i<=min(p,ct2);i++){

            for (int j=0;j<=min(p,ct3);j++){
                temp=sum;
                temp+=(i*2)+(j*6);
                if(temp%9==0) {

                    f=true;
                    break;
                }
            }
            if(f) break;

        }
        if(f) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define int long long

signed main() {
    int tt;
    cin >> tt;
    while (tt--) {
        string s;
        cin >> s;

        int sum = 0, ct2 = 0, ct3 = 0;

        for (char c : s) {
            int digit = c - '0';
            sum += digit;
            if (digit == 2) ct2++;
            if (digit == 3) ct3++;
        }

        bool f = false;
        for (int i = 0; i <= min(10LL, ct2); i++) {
            for (int j = 0; j <= min(10LL, ct3); j++) {
                if ((sum + i * 2 + j * 6) % 9 == 0) {
                    f = true;
                    break;
                }
            }
            if (f) break;
        }

        if (f) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
}

Answer: 2


#include <bits/stdc++.h>

#include <algorithm>

#include <string>

#include <vector>

typedef long long ll;

using namespace std;

int main()

{

    int t;

    cin >> t;

    while(t--)

    {

        string s;

        cin >> s;

        if(s == "5472778912773")

        {

            cout << "NO" << endl;

            continue;

        }

        int twos = 0;

        int threes = 0;

        long long sum = 0;

        for(int i = 0; i < s.size(); i++)

        {

            if(s[i] == '2')twos++;

            if(s[i] == '3')threes++;

            sum += (int)s[i] - 48;

        }

        int fl = 0;

        //cout << twos << " " << threes << " "<< sum << endl;

        for(int i = 0; i <= min(10, twos);i++)

        {

            for(int j = 0; j <= min(10, threes); j++)

            {

                if (((sum + 2*i + 6*j)%9) == 0)

                {

                    fl = 1;

                    //cout << i << " " << j << endl;

                }

            }

        }

        if(fl)

            cout << "YES" << endl;

        else

            cout << "NO" << endl;

    }

}

#include <bits/stdc++.h>

#include <algorithm>

#include <string>

#include <vector>

typedef long long ll;

using namespace std;

int main()

{

    int t;

    cin >> t;

    while(t--)

    {

        string s;

        cin >> s;

        int twos = 0;

        int threes = 0;

        long long sum = 0;

        for(int i = 0; i < s.size(); i++)

        {

            if(s[i] == '2')twos++;

            if(s[i] == '3')threes++;

            sum += (int)s[i];

        }

        int fl = 0;

        for(int i = 0; i <= min(10, twos);i++)

        {

            for(int j = 0; j <= min(10, threes); j++)

            {

                if (((sum + 2*i + 6*j)%9) == 0)

                {

                    fl = 1;

                    cout << i << " " << j << endl;

                }

            }

        }

        if(fl)

            cout << "YES" << endl;

        else

            cout << "NO" << endl;

    }

}

Answer: 1


#include<vector>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;

 while(t--)
 {
 string s;
 cin>>s;

 int rem=0;
int cnt2=0;
int cnt3=0;
 for(int i=0;i<s.length();i++)
 {

         rem=(rem+(s[i]-'0'))%9;

         if(s[i]=='2')
         cnt2++;

        if(s[i]=='3')
         cnt3++;
 }
 int flag=0;
// cout<<rem;
 for(int i=0;i<=min(cnt2,8);i++)
 {
     for(int j=0;j<=min(3,cnt3);j++)
     {
      //   cout<<i<<j<<"\n";
         if((rem+(i*2)%9+(j*6)%9)%9==0)
         {
             cout<<"YES\n";
             flag=1;
             break;
         }
         if(flag)
         break;
     }
 }
 if(!flag)
 cout<<"NO\n";
 }
}

#include<vector>
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;

 while(t--)
 {
 string s;
 cin>>s;

 int rem=0;
int cnt2=0;
int cnt3=0;
 for(int i=0;i<s.length();i++)
 {

         rem=(rem+(s[i]-'0'))%9;

         if(s[i]=='2')
         cnt2++;

        if(s[i]=='3')
         cnt3++;
 }
 int flag=0;
// cout<<rem;
 for(int i=0;i<=min(cnt2,8);i++)
 {
     for(int j=0;j<=min(3,cnt3);j++)
     {
      //   cout<<i<<j<<"\n";
         if((rem+(i*2)%9+(j*6)%9)%9==0)
         {
             cout<<"YES\n";
             flag=1;
             break;
         }

     }
      if(flag)
         break;
 }
 if(!flag)
 cout<<"NO\n";
 }
}

Answer: 2


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdint>
#include <unordered_set>
#include <unordered_map>
using namespace std;

int main(){
    int tc; cin >> tc;
    for(int t = 0; t < tc; t++){
        string s; cin >> s;
        int twos = 0, threes = 0, sum = 0;
        for(char n : s){
            int curr = n - '0';
            sum+=curr; if(curr==2){ twos++; } if(curr==3){ threes++; }
        }
        bool works = false;
        int need = 9 - (sum % 9);
        if(need % 2 == 1){ need+=9; }
        if(need/2 <= twos || (need >= 6 && threes >= 1 && (need-(6*threes > need ? (need%6 == 0 ? 6 :need%6) : 6*threes))/2 <= twos)){ works = true; }
        if(works || need % 9 == 0){ cout << "YES" << endl; } else{ cout << "NO" << endl; }
    }
}

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <cmath>
#include <cstdint>
#include <unordered_set>
#include <unordered_map>
using namespace std;

int main(){
    int tc; cin >> tc;
    for(int t = 0; t < tc; t++){
        string s; cin >> s;
        int twos = 0, threes = 0, sum = 0;
        for(char n : s){
            int curr = n - '0';
            sum+=curr; if(curr==2){ twos++; } if(curr==3){ threes++; }
        }
        bool works = false;
        int need = 9 - (sum % 9);
        if(need % 2 == 1){ need+=9; }
        if(need/2 <= twos || (need >= 6 && threes >= 1 && (need-(6*threes > need ? need - (need % 6) : 6*threes))/2 <= twos)){ works = true; }
        if(works || need % 9 == 0){ cout << "YES" << endl; } else{ cout << "NO" << endl; }
    }
}

Answer: 2


#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define ff first
#define ss second

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input1.txt", "r", stdin);
    //freopen("output1.txt", "w", stdout);
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        int twos=0,sixs=0,sum=0;
        for(int i=0; i<s.size(); i++){
            if(s[i]=='2') twos++;
            if(s[i]=='3') sixs++;
            sum += s[i]-'0';
        }
        if(sum%9 == 0) cout << "YES" << endl;
        else{
            int isum=0, ii, flag=0;
            for(int i=1; isum<=((2*twos)+(6*sixs)); i++){
                isum = (9*i)-sum;
                if(isum < 0){
                    continue;
                }
                if(isum > ((2*twos)+(6*sixs))){
                    break;
                }
                if(isum-(6*sixs) >= 0){
                    isum -= 6*sixs;
                }
                if(isum%2 == 0 and (2*twos)>=(isum-(isum%2))){
                    flag = 1;
                    break;
                }
            }
            if(flag == 1) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define ff first
#define ss second

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    //freopen("input1.txt", "r", stdin);
    //freopen("output1.txt", "w", stdout);
    int t; cin >> t;
    while(t--){
        string s; cin >> s;
        int twos=0,sixs=0,sum=0;
        for(int i=0; i<s.size(); i++){
            if(s[i]=='2') twos++;
            if(s[i]=='3') sixs++;
            sum += s[i]-'0';
        }
        if(sum%9 == 0) cout << "YES" << endl;
        else{
            int flag = 0;
            for(int i=0; i<9; i++){
                if(i > twos) break;
                for(int j=0; j<9; j++){
                    if(j > sixs) break;
                    if((sum + (i*2) + (j*6))%9 == 0){
                        flag = 1;
                        break;
                    }
                }
                if(flag == 1){
                    break;
                }
            }
            if(flag==1) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}

Answer: 2


#include <bits/stdc++.h>

using namespace std;

#define RADHE_RADHE ios_base::sync_with_stdio(false);

#define endl '\n'

int main() {

    RADHE_RADHE;

    int t;

    cin >> t;

    while (t--) {

        // First of all if no.s sum%9==0, then "YES"...

        // If not the case and there is no presence of 2 or 3 then "NO"...

        // Its not required to have more than 8 2's and 3 6's...

        string s;

        cin>>s;

        int n = s.size();

        int cnt2 = 0;

        int cnt3 = 0;

        long long sum = 0;

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

            if(s[i]=='3')cnt3++;

            else if(s[i]=='2')cnt2++;

            sum+=(s[i]-'0');

        }

        if(!cnt3 && !cnt2){

            cout<<"NO"<<endl;

        }

        else{

            bool ok = false;

            for(int i = 0;i<=min(cnt2,8);i++){

                for(int j = 0;j<=min(cnt3,8);j++){

                    if(((sum+2*i+6*j)%9)==0){

                        ok = true;

                    }

                }

            }

            cout<<(ok?"YES":"NO")<<endl;

        }

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define RADHE_RADHE ios_base::sync_with_stdio(false);

#define endl '\n'

int main() {

    RADHE_RADHE;

    int t;

    cin >> t;

    while (t--) {

        // First of all if no.s sum%9==0, then "YES"...

        // If not the case and there is no presence of 2 or 3 then "NO"...

        // Its not required to have more than 8 2's and 3 6's...

        string s;

        cin>>s;

        int n = s.size();

        int cnt2 = 0;

        int cnt3 = 0;

        long long sum = 0;

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

            if(s[i]=='3')cnt3++;

            else if(s[i]=='2')cnt2++;

            sum+=(s[i]-'0');

        }

        if(sum%9==0){

            cout<<"YES"<<endl;

            continue;

        }

        if(!cnt3 && !cnt2){

            cout<<"NO"<<endl;

        }

        else{

            bool ok = false;

            for(int i = 0;i<=min(cnt2,8);i++){

                for(int j = 0;j<=min(cnt3,3);j++){

                    if(((sum+2*i+6*j)%9)==0){

                        ok = true;

                    }

                }

            }

            cout<<(ok?"YES":"NO")<<endl;

        }

    }

    return 0;

}

Answer: 2


#include<bits/stdc++.h>
#define int long long int
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

void solve(){
    string s;   cin >> s;

    int sum = 0;
    int cnt_2 = 0, cnt_3 = 0;
    for(int i = 0; i < s.size(); i++){
        sum += (s[i] - '0');
        if(s[i] == '2') cnt_2++;
        else if(s[i] == '3') cnt_3++;
    }

    for(int i = 0; i <= cnt_2; i++){
        int k = (sum + (i * 2)) % 9;
        if((k % 9 == 0) || (cnt_3 && (k % 9 == 3)) || (cnt_3 >= 2 && (k % 9 == 6))){
            cout << "YES\n";
            return;
        }
    }
    cout << "NO\n";

}

signed main(){
    fast;
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

#include<bits/stdc++.h>
#define int long long int
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

void solve(){
    string s;   cin >> s;
    int sum = 0;
    for(int i = 0; i < s.size(); i++){
        sum += (s[i] - '0');
    }

    int cnt_2 = 0, cnt_3 = 0;
    for(int i = 0; i < s.size(); i++){
        if(s[i] == '2') cnt_2++;
        else if(s[i] == '3') cnt_3++;
    }

    while(1){
        int t = sum;
        if(sum % 9 == 0){
            cout << "YES\n";
            return;
        }

        int d = 18 - (sum % 18);
        if(d & 1){
            d = 9 - (sum % 9);
        }
        int six = min(cnt_3, (d / 6));
        d -= six * 6;
        sum += (six * 6);
        cnt_3 -= six;

        int two = min(cnt_2, (d / 2));
        d -= two * 2;
        sum += (two * 2);
        cnt_2 -= two;

        if(t == sum) break;
    }

    cout << "NO\n";
}

signed main(){
    fast;
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}

Answer: 1


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
int main(){
   ll t,ans=0;
   cin>>t;
   string n;
   for(ll i=0; i<t; i++){
      cin>>n;
      ll m=n.size();
      ans=0;
      map<char, ll> map;
      for(ll i=0; i<m; i++){
         ans+=(n[i]-48);
         if(n[i]=='2'){
            map['2']++;
         }
         else if(n[i]=='3'){
            map['3']++;
         }
      }
      ans=ans%9;
      if(ans==0){
         cout<<"YES"<<"\n";
         continue;
      }
      else if(ans==1){
         if(map['2']>=4){
            cout<<"YES"<<"\n";
            continue;
         }
         else if(map['3']>=1 && map['2']>=1){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==2){
         if(map['3']>=2 && map['2']>=2){
            cout<<"YES"<<"\n";
            continue;
         }
         if(map['2']>=8){
            cout<<"YES"<<"\n";
            continue;
         }
         if(map['3']>=1 && map['2']>=5){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==3){
         if(map['3']>=1){
            cout<<"YES"<<"\n";
            continue;
         }
         else if(map['2']>=3){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==4){
         if(map['3']>=2 && map['2']>=1){
            cout<<"YES"<<"\n";
            continue;
         }
         else if(map['2']>=7){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==5){
         if(map['2']>=2){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==6){
         if(map['3']>=2 ||map['2']>=6 ){
            cout<<"YES"<<"\n";
            continue;
         }
         else if(map['3']>=1 && map['2']>=3){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==7){
         if(map['2']>=1){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      else if(ans==8){
         if(map['2']>=5){
            cout<<"YES"<<"\n";
            continue;
         }
         else if(map['3']>=1 && map['2']>=2){
            cout<<"YES"<<"\n";
            continue;
         }
      }
      cout<<"NO"<<"\n";
   }
   return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;

#define int long long int

int m = 1;

int find9(string x, int i, int n, int m){

	// cerr << i << "-"<<x <<", ";

	// check divisible

	int d = 0;
	for (int i = 0; i < n; ++i)
	{
		int s = (x[i]-'0') + d*10;

		d = s%9;
	}

	if(d == 0){
		return 1;
	}

	if(i >= n){
		return 0;
	}

	// cerr << m;

	// cerr << (x/(m*1)) << " ..";

	if( (x[i]-'0') < 4 && (x[i]-'0') > 1 ){

		if((x[i]-'0') == 2){

			int a1 = find9(x, i+1, n, m);

			x[i] = (4+'0');

			int a2 = find9(x, i+1, n, m);

			return (a1||a2);
		}
		else{
			int a1 = find9(x, i+1, n, m);

			x[i] = (9+'0');

			int a2 = find9(x, i+1, n, m);

			return (a1||a2);
		}

	}
	else
	return find9(x, i+1, n, m);
}

void solve(){
	string x;
	cin >> x;

	int ans = 0;

	int n = x.size();

	ans = find9(x, 0, n, 1);

	if(ans){
		cout<< "YES"<< endl;
	}
	else{
		cout<< "NO" << endl;
	}
}

int32_t main(){
	string s;
	int x = 0;

	// for(int i = 0; i<43*2+1; i++)
	// getline(cin, s);

	// int i = 0;
	// while(s[i] != '\0'){
	// 	cout<< s[i];
	// 	i++;
	// }

	// cout<< s << endl;

	int t = 0;

    cin>> t;
    while(t--){
    	solve();
    }

    return 0;
}

Answer: 1


#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while (t>0){
        t--;
        string s;
        cin>>s;
        int sum=0;
        int n=s.length();
        int two=0,three=0;
        for (int i=0;i<n;i++){
            sum=sum + (int)s[i]-48;
            if (s[i]=='2'){
                two+=1;
            }else if(s[i]=='3'){
                three+=1;
            }
        }
        if (sum%9==0){
            cout<<"YES"<<endl;
        }
        else{
            int f=0;
            for (int i=0;i<=min(100000,two);i++){
                for (int j=0;j<=min(100000,three);j++){
                    long long m=i*2+j*6;
                    if (m%9==9-sum%9){
                        f=1;
                        break;
                    }
                }
            }
            if (f){
                cout<<"yes"<<endl;
            }else{
                cout<<"no"<<endl;
            }
    }
}
}

#include<iostream>
using namespace std;
int main(){
    int t;
    cin>>t;
    while (t>0){
        t--;
        string s;
        cin>>s;
        int sum=0;
        int n=s.length();
        int two=0,three=0;
        for (int i=0;i<n;i++){
            sum=sum + (int)s[i]-48;
            if (s[i]=='2'){
                two+=1;
            }else if(s[i]=='3'){
                three+=1;
            }
        }
        if (sum%9==0){
            cout<<"YES"<<endl;
        }
        else{
            int b=9-sum%9;
            if (b%2==0){
                if (b==8 &&((two>=1 && three>=1) || (two>=4))){
                    cout<<"YES"<<endl;
                }else if (b==6 && ((three>=1)||(two>=3))){
                    cout<<"YES"<<endl;
                }else if (b==4 && two>=2){
                    cout<<"YES"<<endl;
                }else if (b==2 && two>=1){
                    cout<<"YES"<<endl;
                }else{
                    cout<<"NO"<<endl;
                }
            }else{
                b=b+9;
                if (b==16 && two>=2){
                    if ((three==1 && two>=5)|| (three>1)){
                        cout<<"YES"<<endl;
                    }else{
                        cout<<"NO"<<endl;
                    }
                }else if (b==14 && two>=1){
                    if ((three==1 && two>=4)|| (three>=2)){
                        cout<<"YES"<<endl;
                    }else{
                        cout<<"NO"<<endl;
                    }
                }else if (b==12){
                    if ((three==1 && two>=3) || (three>=2)){
                        cout<<"YES"<<endl;
                    }else{
                        cout<<"NO"<<endl;
                    }
                }else if (b==10 && two>=2){
                    if ((three==1)|| (two>=5)){
                        cout<<"YES"<<endl;
                    }else{
                        cout<<"NO"<<endl;
                    }
                }else {
                    cout<<"NO"<<endl;
                }
            }
        }
    }
    return 0;
}

Answer: 1


#include<bits/stdc++.h>
using namespace std;

#define add emplace_back
#define FOR(i, a, b) for(int i = a;i < (b); ++i)
#define FORD(i , a, b) for(int i = a;i >= (b);i--)
#define all(v) v.begin(), v.end()
#define len(x) (x).size();

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;

const int mod = 1e9+7;

void Main(){
	string s;
	cin >> s;
	int sum = 0, two = 0, three = 0;

	for(int i = 0;i < s.size(); i++){
		sum = (sum + (s[i]-48)) % 9;
		two += (s[i]=='2');
		three += (s[i]=='3');
	}
	if(sum==0) return void( cout << "YES" << "\n");
	bool ok = 0;
	for(int i = 0;i <= min(two, 6); i++){
		for(int j = 0;j <= min(three, 4); j++){
			if((sum+2*i+6*j)%9 == 0) ok = 1;
		}
	}
	cout << (ok?"YES":"NO") << "\n";
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.tie(NULL);

	int t;
	for(cin>>t; t; t--) Main();

	return 0;
}

#include<bits/stdc++.h>
using namespace std;

#define add emplace_back
#define FOR(i, a, b) for(int i = a;i < (b); ++i)
#define FORD(i , a, b) for(int i = a;i >= (b);i--)
#define all(v) v.begin(), v.end()
#define len(x) (x).size();

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;
using vll = vector<ll>;

const int mod = 1e9+7;

void Main(){
	string s;
	cin >> s;
	int sum = 0, two = 0, three = 0;

	for(int i = 0;i < s.size(); i++){
		sum = (sum + (s[i]-48)) % 9;
		two += (s[i]=='2');
		three += (s[i]=='3');
	}
	if(sum==0) return void( cout << "YES" << "\n");
	bool ok = 0;
	for(int i = 0;i <= min(two, 100); i++){
		for(int j = 0;j <= min(three, 100); j++){
			if((sum+2*i+6*j)%9 == 0) ok = 1;
		}
	}
	cout << (ok?"YES":"NO") << "\n";
}

int main(){
	cin.tie(0)->sync_with_stdio(0);
	cin.tie(NULL);

	int t;
	for(cin>>t; t; t--) Main();

	return 0;
}

Answer: 2


#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int t;
int a[N];
int c;
int x,y;
int sum;
bool f=0;
int main(){
	cin>>t;
	while(t--){
		x=0,y=0;
		for(int i=1;i<=N;i++){
			a[i]=0;
		}
		string s;
		cin>>s;
		c=s.size();
		sum=0;
		f=0;
		for(int i=0;i<c;i++){
			a[i+1]=int(s[i]-'0');
			sum+=a[i+1];
			if(a[i+1]==2){
				x++;
			}
			if(a[i+1]==3){
				y++;
			}
		}
		for(int i=0;i<=x;i++){
			for(int j=0;j<=y;j++){
				if((sum+2*i+6*j)%9==0){
					f=1;
					break;
				}
			}
			if(f==1)break;
		}
		if(f==0){
			cout<<"No"<<endl;
		}else{
			cout<<"Yes"<<endl;
		}
	}
	return 0;
}

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int t;
int c;
int x,y;
int sum;
bool f=0;
int main(){
	cin>>t;
	while(t--){
		string s;
		cin>>s;
		c=s.size();
		sum=0;
		f=0;
		x=0,y=0;
		for(int i=0;i<c;i++){
			sum+=int(s[i]-'0');
			if(int(s[i]-'0')==2){
				x++;
			}
			if(int(s[i]-'0')==3){
				y++;
			}
		}
		sum%=9;
		if(sum==0){
			cout<<"Yes"<<endl;
			continue;
		}
		for(int i=0;i<=x;i++){
			for(int j=0;j<=y;j++){
				if((sum+2*i+6*j)%9==0){
					f=1;
					break;
				}
			}
			if(f==1)break;
		}
		if(f==0){
			cout<<"No"<<endl;
		}else{
			cout<<"Yes"<<endl;
		}
	}
	return 0;
}

Answer: 2


 #include <bits/stdc++.h>

using namespace std;

#define int long long

signed main () {

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

    int tt; cin >> tt;

    while (tt--) {

        string s; cin >> s;

        int n = s.size();

        vector<int> a (n);

        for(int i = 0; i < n; i++) a[i] = s[i] - '0';

        vector<vector<int>> dp (n + 1 , vector<int> (10 , 0));

        dp[0][a[0]] = 1;

        if(a[0] * a[0] < 10) dp[0][(a[0] * a[0]) % 9] = 1;

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

          for(int j = 0; j < 10; j++){

              if(dp[i - 1][j]){

                  dp[i][(j + a[i]) % 9] = 1;

                 if(a[i] * a[i] < 10) dp[i][(j + a[i] * a[i]) % 9] = 1;

              }

          }

        }

        cout << (dp[n - 1][0] ? "YES\n" : "NO\n");

    }

    return 0;

}

#include <bits/stdc++.h>

using namespace std;

#define int long long

signed main () {

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

    int tt; cin >> tt;

    while (tt--) {

        string s; cin >> s;

        int n = s.size();

        vector<int> a (n);

        for(int i = 0; i < n; i++) a[i] = s[i] - '0';

        vector<vector<int>> dp (n + 1 , vector<int> (10 , 0));

        dp[0][a[0] % 9] = 1;

        if(a[0] * a[0] < 10) dp[0][(a[0] * a[0]) % 9] = 1;

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

          for(int j = 0; j < 10; j++){

              if(dp[i - 1][j]){

                  dp[i][(j + a[i]) % 9] = 1;

                 if(a[i] * a[i] < 10) dp[i][(j + a[i] * a[i]) % 9] = 1;

              }

          }

        }

        cout << (dp[n - 1][0] ? "YES\n" : "NO\n");

    }

    return 0;

}

Answer: 2


#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve(){
	string s;
	cin >> s;
	ll n = s.size();
	s = '?' + s;
	ll n2 = 0;
	ll n3 = 0;
	ll sum = 0;
	for(ll i = 1 ; i <= n ; ++i){
		if(s[i] == '2') n2++;
		if(s[i] == '3') n3++;
		sum += s[i] - '0';
	}
	if(sum % 9 == 0){
		cout << "YES" << endl;
		return;
	}
	for(ll i = 0 ; i <= n2 ; ++i){
		for(ll j = 0 ; j <= n3 ; ++j){
			ll tsum = sum + i * 2 + j * 6;
			if(tsum % 9 == 0){
				cout << "YES" << endl;
				return;
			}
		}
	}
	cout << "NO" << endl;
}

signed main(){
	ll t;
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long

void solve(){
	string s;
	cin >> s;
	ll n = s.size();
	s = '?' + s;
	ll n2 = 0;
	ll n3 = 0;
	ll sum = 0;
	for(ll i = 1 ; i <= n ; ++i){
		if(s[i] == '2') n2++;
		if(s[i] == '3') n3++;
		sum += s[i] - '0';
	}
	if(sum % 9 == 0){
		cout << "YES" << endl;
		return;
	}
	for(ll i = 0 ; i <= n2 ; ++i){
		for(ll j = 0 ; j <= n3 ; ++j){
			sum += i * 2 + j * 6;
			if(sum % 9 == 0){
				cout << "YES" << endl;
				return;
			}
		}
	}
	cout << "NO" << endl;
}

signed main(){
	ll t;
	cin >> t;
	while(t--){
		solve();
	}
	return 0;
}

Answer: 1


def solve(num):
    three = 0
    two = 0
    s = 0
    s2 = 0
    for i in num:
        if i == '2':
            two += 1

        if i == '3':
            s2 += 9
        else:
            s2 += int(i)
        s += int(i)

    if s%9 == 0 or s2%9==0:
        return "YES"

    if two == 0:
        return "NO"

    for i in range(1,two + 1):
        if (2*i + s ) % 9 == 0 or (2*i + s2 ) % 9 == 0 :
            return "YES"
    return "NO"

for _ in range(int(input())):
    print(solve(input()))

def solve(num):
    three = 0
    two = 0
    s = 0
    for i in num:
        if i == '2':
            two += 1
        elif i == '3':
            three += 1
        s += int(i)

    if s % 9 == 0:
        return "YES"

    for i in range(min(9,two+1)):
        for j in range(min(7,three+1)):
            if (s + 2*i + 6*j ) % 9 == 0:
                return "YES"
    return "NO"

for _ in range(int(input())):
    print(solve(input()))

Answer: 2


#include<bits/stdc++.h>
using namespace std ;

typedef unsigned long long ull ;
typedef long long ll ;
typedef long double ld ;

const ll mod = 1e9+7;
const ld eps = 1e-9 ;
const ll maxn = 1e5+1;
const ll inf = 1e15;
const ll minf = -inf;

#define int long long
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()

void solve(){
    string s ; cin >> s ;
    int n = s.size() ;

    int sum = 0 ;
    int two = 0 , three = 0 ;

    for(int i=0;i<n;i++){
        if(s[i] == '2')two++ ;
        if(s[i] == '3')three++ ;
        sum += s[i]-'0' ;
    }

    for(int i=0;i<min(10ll , two+1);i++){
        for(int j=0;j<min(10ll , three+1);j++){
            if((sum + i*2 + j*6) %9 == 0){
                cout << "YES" << endl ;
                return ;
            }
        }
    }
    cout << "NO" << endl ;
    return ;
}

int32_t main(){

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

   int t = 1; cin >> t;
    for(int i=1; i<=t; i++){
        //cout<<"Case #"<<i<<": \n";
        solve();
    }
}

#include<bits/stdc++.h>
using namespace std ;

typedef unsigned long long ull ;
typedef long long ll ;
typedef long double ld ;

const ll mod = 1e9+7;
const ld eps = 1e-9 ;
const ll maxn = 1e5+1;
const ll inf = 1e15;
const ll minf = -inf;

#define int long long
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()

void solve(){
    string s ; cin >> s ;
    int n = s.size() ;

    int sum = 0 ;
    int two = 0 , three = 0 ;

    for(int i=0;i<n;i++){
        if(s[i] == '2')two++ ;
        if(s[i] == '3')three++ ;
        sum += s[i]-'0' ;
    }

    if(sum%9 == 0){
        cout << "YES" << endl ;
        return ;
    }

    for(int i=0;i<min(8ll , two+1);i++){
        for(int j=0;j<min(8ll , three+1);j++){
            if((sum + i*2 + j*6) %9 == 0){
                cout << "YES" << endl ;
                return ;
            }
        }
    }
    cout << "NO" << endl ;
    return ;
}

int32_t main(){

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

   int t = 1; cin >> t;
    for(int i=1; i<=t; i++){
        //cout<<"Case #"<<i<<": \n";
        solve();
    }
}

Answer: 1


import sys

input_data = sys.stdin.read().strip()

lines = input_data.splitlines()
lines.pop(0)

inp=[]
ans=[]
k=0
for line in lines:
   inp.append(int(line))

def get_digits(number):
    return [int(digit) for digit in str(abs(number))]

def count(dig):
    three = 0
    two = 0
    for d in dig:
        if d == 2:
            two += 1
        if d == 3:
            three += 1
    return two, three

for num in inp:
    nta = (num % 9)
    digits = get_digits(num)
    q2, q3 = count(digits)
    i, j = 0, 0
    f = 0
    while i <= q3:
        while j <= q2:
            if (nta + 6*i + j) % 9 == 0:
                ans.append("YES")
                f = 1
                break
            j += 1
        if f == 1:
            break
        j = 0
        i += 1
    if f == 0:
        ans.append("NO")

for a in ans:
    print(a)

import sys

input_data = sys.stdin.read().strip()

lines = input_data.splitlines()
lines.pop(0)

inp=[]
ans=[]
k=0
for line in lines:
   inp.append(int(line))

def get_digits(number):
    return [int(digit) for digit in str(abs(number))]

def count(dig):
    three = 0
    two = 0
    for d in dig:
        if d == 2:
            two += 1
        if d == 3:
            three += 1
    return two, three

for num in inp:
    nta = (num % 9)
    digits = get_digits(num)
    q2, q3 = count(digits)
    i, j = 0, 0
    f = 0
    while i <= q3:
        while j <= q2:
            if (nta + 6*i + 2*j) % 9 == 0:
                ans.append("YES")
                f = 1
                break
            j += 1
        if f == 1:
            break
        j = 0
        i += 1
    if f == 0:
        ans.append("NO")

for a in ans:
    print(a)

Answer: 2


#pragma GCC optimize("Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
void solve() {
    string n;
    cin>>n;
    ll b = 0, c = 0;
    ll sum = 0;
    for(ll i = 0;i<n.size();i++) {
        sum+=(n[i]-'0');
        if(n[i]=='2') b++;
        else if(n[i]=='3') c++;
    }
    for(ll i = 0;i<=b;i++) {
        for(ll x = 0;x<=c;x++) {
            if((sum+(i*2)+(x*6))%9==0) {
                cout<<"YES"<<"\n";
                return;
            }
        }
    }
    cout<<"NO"<<"\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    for(ll j = 0;j<t;j++){
        solve();
    }
}

#pragma GCC optimize("Ofast", "inline")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define M 1000000007
void solve() {
    ll n;
    cin>>n;
    map<ll,ll> mp;
    ll sum = 0;
    while(n>0) {
        ll a = n%10;
        n/=10;
        mp[a]++;
        sum+=a;
    }
    ll b = mp[2];
    ll c = mp[3];
    for(ll i = 0;i<=b;i++) {
        for(ll x = 0;x<=c;x++) {
            if((sum+(i*2)+(x*6))%9==0) {
                cout<<"YES"<<"\n";
                return;
            }
        }
    }
    cout<<"NO"<<"\n";
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll t;
    cin>>t;
    for(ll j = 0;j<t;j++){
        solve();
    }
}

Answer: 1