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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// clang-format off
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(auto i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<"("<<p.first<<", "<<p.second<<")";}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<"}";}
#ifdef DEBUG
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<"\n";}(X)
#else
#define debug(...) {}
#endif
// clang-format on

#include <ext/pb_ds/assoc_container.hpp>
using htable_t       = __gnu_pbds::gp_hash_table<LL, int>;

const int nbase      = 10;
const int max_digits = 18;

// Representation of digits multiplication
#define GET_BIT(a, i) (((a) >> i) & 1)
#define SET_BIT(a, i) (a ^= (1 << (i)))
using fact_t                      = array<short, 4>;
using hash_t                      = uint32_t;
const array<LL, 4> fact_primes    = {2, 3, 5, 7};
const array<hash_t, 4> fact_nexps = {55, 37, 19, 19};
const array<hash_t, 4> fact_pos
  = {1,
     fact_nexps[0],
     fact_nexps[0] * fact_nexps[1],
     fact_nexps[0] * fact_nexps[1] * fact_nexps[2]};
const array<fact_t, nbase> fact_digits = {{
  {0, 0, 0, 0},  // 0
  {0, 0, 0, 0},  // 1
  {1, 0, 0, 0},  // 2
  {0, 1, 0, 0},  // 3
  {2, 0, 0, 0},  // 4
  {0, 0, 1, 0},  // 5
  {1, 1, 0, 0},  // 6
  {0, 0, 0, 1},  // 7
  {3, 0, 0, 0},  // 8
  {0, 2, 0, 0}   // 9
}};
#define HDELTA_ROW(c)                                                          \
    fact_pos[0] * fact_digits[c][0] + fact_pos[1] * fact_digits[c][1]          \
      + fact_pos[2] * fact_digits[c][2] + fact_pos[3] * fact_digits[c][3]
const array<hash_t, nbase> fact_hdelta = {HDELTA_ROW(0),
                                          HDELTA_ROW(1),
                                          HDELTA_ROW(2),
                                          HDELTA_ROW(3),
                                          HDELTA_ROW(4),
                                          HDELTA_ROW(5),
                                          HDELTA_ROW(6),
                                          HDELTA_ROW(7),
                                          HDELTA_ROW(8),
                                          HDELTA_ROW(9)};
const int nhashes                      = 695971;
const int zero_id                      = 36100;
const int nids                         = zero_id + 1;

LL fact_to_mul(const fact_t& fact);
hash_t fact_to_hash(const fact_t& fact);
fact_t hash_to_fact(const hash_t& hash);
fact_t mul_to_fact(LL mul);
int mul_to_id(const LL& mul);
LL digit_mul(LL mul);

array<int, nhashes> hash_to_id;
array<hash_t, nids> id_to_hash;
array<short, nids> id_to_group;
void gen_muls();
void gen_id_to_group();

using decimal_t = vector<pair<int, LL>>;
decimal_t N_to_decimal(LL N);
array<array<array<LL, nbase>, nids>, max_digits> dp;
array<array<LL, nids>, max_digits> dp2;
void calc_dp();
LL lower_with_id(const decimal_t& N, int id);

int
main()
{
    cin.tie(0)->sync_with_stdio(0);
    gen_muls();
    gen_id_to_group();
    calc_dp();

    // Get ids that don't belong to group 0
    vector<int> non0ids;
    REP (id, nids)
        if (id_to_group[id] != 0) non0ids.emplace_back(id);

    int t;
    cin >> t;
    while (t--) {
        LL N;
        cin >> N;

        array<LL, nbase> score;
        score.fill(0);

        // Handle 10^18
        score[id_to_group[mul_to_id(digit_mul(N))]] += 1;
        if (N == 1000000000000000000)
            score[id_to_group[mul_to_id(digit_mul(--N))]] += 1;

        // Add influence to score of each id
        LL add_to_0 = N - 1;
        auto dec = N_to_decimal(N);
        for (const auto& id : non0ids) {
            const auto &val = lower_with_id(dec, id);
            score[id_to_group[id]] += val, add_to_0 -= val;
        }
        score[0] += add_to_0;

        // Output
        REP (i, nbase) cout << score[i] << " ";
        cout << "\n";
    }
    return 0;
}

LL
lower_with_id(const decimal_t& N, int id)
{
    LL results              = 0;
    bool dont_care_about_tz = false;
    for (int n = ssize(N) - 1; n >= 0; --n, dont_care_about_tz = true) {
        const auto& [digit, pref] = N[n];

        // Handle digit multiplying to 0
        if (id == zero_id) {
            if (digit == 0) {
                results += pref;
                break;
            }

            if (digit - 1 >= 1) results += dp[n][zero_id][digit - 1];
            if (dont_care_about_tz)
                results += dp[n][zero_id][0];
            else
                results += dp2[n][zero_id];
            continue;
        }

        // It's impossible to have non-zero id with digit == 0
        if (digit == 0) break;

        // Smaller digit here, so definitely smaller in the future
        if (digit - 1 >= 1) results += dp[n][id][digit - 1];
        if (!dont_care_about_tz) results += dp2[n][id];

        const auto& h = id_to_hash[id];
        if (!GET_BIT(h, digit)) break;
        id = hash_to_id[(h >> nbase) - fact_hdelta[digit]];
    }
    return results;
}

void
calc_dp()
{
    LL pow10 = 10;
    dp2[0].fill(0);
    REP (c, nbase) dp[0][mul_to_id(c)][c] = 1;
    FOR (n, 1, max_digits - 1) {
        dp2[n] = dp2[n - 1];

        REP (id, nids - 1) {
            const auto& h = id_to_hash[id];
            dp[n][id].fill(0);

            FOR (c, 1, nbase - 1) {
                if (!GET_BIT(h, c)) continue;
                auto idp = hash_to_id[(h >> nbase) - fact_hdelta[c]];
                REP (cp, nbase) dp[n][id][c] += dp[n - 1][idp][cp];
                dp2[n][id] += dp[n - 1][id][c];
            }
        }

        // Handle zero_id
        LL dpzerosum = 0;
        FOR (c, 1, nbase - 1) dpzerosum += dp[n - 1][zero_id][c];
        dp2[n][zero_id] += dpzerosum;
        dp[n][zero_id].fill(0);
        dp[n][zero_id][0] = pow10;
        FOR (c, 1, nbase - 1)
            dp[n][zero_id][c] = dpzerosum + dp[n - 1][zero_id][0];
        pow10 *= 10;
    }

    // Make dp[n][id][c] = sum k 1...c dp[n][id][k]
    REP (n, max_digits) {
        REP (id, nids) {
            auto& d = dp[n][id];
            FOR (c, 2, nbase - 1) d[c] += d[c - 1];
        }
    }
}

decimal_t
N_to_decimal(LL N)
{
    decimal_t dec;
    for (LL pow10 = 1, pref = 0; N; N /= 10, pow10 *= 10) {
        const auto& digit = N % 10;
        dec.emplace_back(digit, pref);
        pref += pow10 * digit;
    }
    return dec;
}

short
get_group_rec(int id)
{
    if (id_to_group[id] == -1)
        id_to_group[id] = get_group_rec(mul_to_id(
          digit_mul(fact_to_mul(hash_to_fact(id_to_hash[id] >> nbase)))));
    return id_to_group[id];
}

void
gen_id_to_group()
{
    id_to_group.fill(-1);
    REP (x, nbase) id_to_group[mul_to_id(x)] = x;
    REP (id, nids) id_to_group[id] = get_group_rec(id);
}

bool
mul_achievable(LL mul)
{
    if (mul == 0) return 0;
    int digits = 0;
    for (int c = nbase - 1; c > 1; --c)
        while (mul % c == 0) ++digits, mul /= c;
    return digits <= max_digits;
}

bool
fact_have_c(const fact_t& fact, const int& c)
{
    return fact[0] >= fact_digits[c][0] && fact[1] >= fact_digits[c][1]
           && fact[2] >= fact_digits[c][2] && fact[3] >= fact_digits[c][3];
}

void
gen_muls()
{
    LL max_val = 1;
    int id     = 0;
    REP (i, 18) max_val *= 9;
    for (LL val2 = 1; val2 <= max_val; val2 *= 2) {
        for (LL val3 = val2; val3 <= max_val; val3 *= 3) {
            for (LL val5 = val3; val5 <= max_val; val5 *= 5) {
                for (LL val7 = val5; val7 <= max_val; val7 *= 7) {
                    if (!mul_achievable(val7)) continue;
                    auto fact      = mul_to_fact(val7);
                    auto h         = fact_to_hash(fact);
                    hash_to_id[h]  = id;
                    id_to_hash[id] = h << nbase;
                    REP (c, nbase)
                        if (fact_have_c(fact, c)) SET_BIT(id_to_hash[id], c);
                    ++id;
                }
            }
        }
    }
}

LL
fact_to_mul(const fact_t& fact)
{
    LL mul = 1;
    REP (pi, ssize(fact_primes))
        REP (exp, fact[pi]) mul *= fact_primes[pi];
    return mul;
}

hash_t
fact_to_hash(const fact_t& fact)
{
    return fact_pos[0] * fact[0] + fact_pos[1] * fact[1] + fact_pos[2] * fact[2]
           + fact_pos[3] * fact[3];
}

fact_t
hash_to_fact(const hash_t& hash)
{
    return {static_cast<short>((hash % fact_pos[1])),
            static_cast<short>((hash % fact_pos[2]) / fact_pos[1]),
            static_cast<short>((hash % fact_pos[3]) / fact_pos[2]),
            static_cast<short>((hash) / fact_pos[3])};
}

fact_t
mul_to_fact(LL mul)
{
    // FIXME: handle this
    assert(mul != 0);

    fact_t fact = {0, 0, 0, 0};
    REP (pi, ssize(fact_primes)) {
        const auto& prime = fact_primes[pi];
        while (mul % prime == 0) mul /= prime, ++fact[pi];
    }
    return fact;
}

int
mul_to_id(const LL& mul)
{
    return (mul == 0 ? zero_id : hash_to_id[fact_to_hash(mul_to_fact(mul))]);
}

LL
digit_mul(LL mul)
{
    if (mul == 0) return 0;
    LL nxt = 1;
    for (; mul; mul /= 10) nxt *= mul % 10;
    return nxt;
}