1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define all(x) (x).begin(),(x).end()
using namespace __gnu_pbds;
using namespace std;

using ll = long long;
using ld = long double;

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 19;

ll p[11][nmax * 3];
ll dp[nmax + 1][nmax * 3 + 1][nmax * 2 + 1][nmax + 1][nmax + 1]; // in lipsa obligatiilor, si fara 0uri lol (primul pas sa fie nontrivial)

vector<pair<ll, ll>> allpossibles[nmax + 1], pw2[nmax + 1], pw5[nmax + 1];
ll invariant[nmax + 1][11];
ll nullified[nmax + 1], null2[nmax + 1], null5[nmax + 1];

struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return x ^ FIXED_RANDOM;
    }
};

unordered_map<ll, char> calc;


int getred(int x) {
   if(x < 10) return x;
   if(calc.find(x) != calc.end()) return calc[x];
   int pr = 1, cpy = x;
   while(x > 0) pr *= x % 10, x /= 10;
   return calc[cpy] = getred(pr);
}


ll calc0(string n) {
   ll cnt = 0;
   for(int i = sz(n) - 2; i > 0; i--) {
      cnt += 9 * (p[10][i - 1] - p[9][i - 1]);
   }
   for(int i = sz(n) - 1; i > 0; i--) {
      if(n[i] == '0') { 
         int V = 0;
         for(int j = i - 1; j > 0; j--) V = V * 10 + n[j] - '0';
         cnt += V;
         break;
      }
      if(i != sz(n) - 1) cnt += p[10][i - 1]; 
      cnt += (p[10][i - 1] - p[9][i - 1]) * (n[i] - '0' - 1); // -'1'
   }
   return cnt;
}

void testcase() {
   stringstream CTS;
   
   ll A, _0 = 0;
   string n;
   cin >> A;
   
   if(A == 1'000'000'000'000'000'000) _0++, A--;
   CTS << A;
   CTS >> n;
   
   reverse(all(n));
   n = "$" + n;
   _0 += calc0(n);
   
   ll prod = 1;
   
   int rez[11]; fill(rez, rez + 11, 0);
   
   for(int i = sz(n) - 1; i > 1; i--) { // 0uri pe prefix
      rez[0] += nullified[i - 1];
      for(int j = 0; j < 10; j++) rez[j] += invariant[i - 1][j];
   }
   
   for(int i = sz(n) - 1; i > 0; i--) {
      //cerr << "raz\n";
      if(prod % 10 == 0)
         rez[0] += p[9][i - 1] * max(0ll, (int)n[i] - '1');
      else {
         rez[0] += nullified[i - 1] * max(0ll, (int)n[i] - '1');
         for(int j = 1; j < n[i] - '0'; j++) {
            if(((prod * j) % 2 != 0 && (prod * j) % 5 != 0)) {
               for(auto [V, freq] : allpossibles[i - 1]) {
                  rez[getred(prod * j * V)] += freq;
               }
            }
            else if((prod * j) % 2 == 0) {
               for(auto [V, freq] : pw2[i - 1])
                  rez[getred(prod * j * V)] += freq;
               rez[0] += null5[i - 1];
            }
            else if((prod * j) % 5 == 0) {
               for(auto [V, freq] : pw5[i - 1])
                  rez[getred(prod * j * V)] += freq;
               rez[0] += null2[i - 1];
            }
         }
      }
      //cerr << "i dwa\n";
      prod *= n[i] - '0';
      if(prod == 0) break;
   }
   rez[getred(prod)]++;
   rez[0] += _0;
   
   for(int i = 0; i < 10; i++) cout << rez[i] << ' ';
   cout << '\n';
}

constexpr void precalc() {
   {
      p[2][0] = p[3][0] = p[5][0] = p[7][0] = p[9][0] = p[10][0] = 1;
      for(int i = 1; i < nmax; i++) p[10][i] = p[10][i - 1] * 10;
      for(int i = 1; i < nmax; i++) p[9][i] = p[9][i - 1] * 9;
      for(int i = 1; i < nmax; i++) p[7][i] = p[7][i - 1] * 7;
      for(int i = 1; i < nmax; i++) p[5][i] = p[5][i - 1] * 5;
      for(int i = 1; i < nmax * 2; i++) p[3][i] = p[3][i - 1] * 3;
      for(int i = 1; i < nmax * 3; i++) p[2][i] = p[2][i - 1] * 2;
   }
   {
      dp[0][0][0][0][0] = 1;
      allpossibles[0].emplace_back(1, 1);
      pw2[0].emplace_back(1, 1);
      pw5[0].emplace_back(1, 1);
      for(int i = 1; i < nmax; i++) {
         for(int _2 = 0; _2 <= i * 3; _2++) {
            for(int _3 = 0; _3 <= i * 2; _3++) {
               for(int _5 = 0; _5 + max((_2 + 2) / 3, (_3 + 1) / 2) <= i; _5++) {
                  for(int _7 = 0; _7 + _5 + max((_2 + 2) / 3, (_3 + 1) / 2) <= i; _7++) {
                     dp[i][_2][_3][_5][_7] += dp[i - 1][_2][_3][_5][_7]; // pui 1
                     if(_2 > 0) dp[i][_2][_3][_5][_7] += dp[i - 1][_2 - 1][_3][_5][_7]; // pui 2
                     if(_3 > 0) dp[i][_2][_3][_5][_7] += dp[i - 1][_2][_3 - 1][_5][_7]; // pui 3
                     if(_2 > 1) dp[i][_2][_3][_5][_7] += dp[i - 1][_2 - 2][_3][_5][_7]; // pui 4
                     if(_5 > 0) dp[i][_2][_3][_5][_7] += dp[i - 1][_2][_3][_5 - 1][_7]; // pui 5
                     if(_2 > 0 && _3 > 0) dp[i][_2][_3][_5][_7] += dp[i - 1][_2 - 1][_3 - 1][_5][_7]; // pui 6
                     if(_7 > 0) dp[i][_2][_3][_5][_7] += dp[i - 1][_2][_3][_5][_7 - 1]; // pui 7
                     if(_2 > 2) dp[i][_2][_3][_5][_7] += dp[i - 1][_2 - 3][_3][_5][_7]; // pui 8
                     if(_3 > 1) dp[i][_2][_3][_5][_7] += dp[i - 1][_2][_3 - 2][_5][_7]; // pui 9
                     
                     if(dp[i][_2][_3][_5][_7] > 0) {
                        if(_2 > 0 && _5 > 0)
                           nullified[i] += dp[i][_2][_3][_5][_7];
                        else {
                           allpossibles[i].emplace_back(p[2][_2] * p[3][_3] * p[5][_5] * p[7][_7], dp[i][_2][_3][_5][_7]);
                           invariant[i][getred(p[2][_2] * p[3][_3] * p[5][_5] * p[7][_7])] += dp[i][_2][_3][_5][_7];
                           if(_5 == 0) pw2[i].emplace_back(p[2][_2] * p[3][_3] * p[5][_5] * p[7][_7], dp[i][_2][_3][_5][_7]);
                           if(_2 == 0) pw5[i].emplace_back(p[2][_2] * p[3][_3] * p[5][_5] * p[7][_7], dp[i][_2][_3][_5][_7]);
                           if(_2 > 0) null2[i] += dp[i][_2][_3][_5][_7];
                           if(_5 > 0) null5[i] += dp[i][_2][_3][_5][_7];
                        }
                     }
                  }
               }
            }
         }
      }
   }
}

signed main() {
   precalc();
   //for(int i= 1; i <= 56; i++) cerr << i << ' ' << getred(i) << '\n';
   cin.tie(0) -> sync_with_stdio(0);
   int T;
   cin >> T;
   //cerr << "niger\n";
   while(T--) testcase();
   
   
   
}

/**
      Binecuvanteaza Doamne Ukraina.
--
*/