fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. string s;
  10. cin >> s;
  11. int n = s.size();
  12.  
  13. int two = 0, three = 0;
  14. int sum = 0;
  15. for(int i = 0; i < n; i++){
  16. sum += s[i] - '0';
  17. if(s[i] == '2')two++;
  18. if(s[i] == '3')three++;
  19. }
  20.  
  21. for(int i = 0; i <= two; i++){
  22. for(int j = 0; j <= three; j++){
  23. int s = 2 * i + 6 * j;
  24. s %= 9;
  25. if((sum + s) % 9 == 0){
  26. cout << "YES\n";
  27. return;
  28. }
  29. }
  30. }
  31. cout << "NO\n";
  32.  
  33. }
  34.  
  35. int main(){
  36. ios_base::sync_with_stdio(false);
  37. cin.tie(nullptr);
  38.  
  39. int t = 1;
  40. cin >> t;
  41.  
  42. for(int i = 1; i <= t; i++){
  43. solve();
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0.01s 5284KB
stdin
9
123
322
333333333333
9997
5472778912773
1234567890
23
33
52254522632
stdout
NO
YES
YES
NO
NO
YES
NO
YES
YES