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
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstring>

#include <array>
#include <vector>

using llu = long long unsigned int;
using histogram_t = std::array<llu, 10>;

static const llu POWERS_OF_TEN[] = {
    1ULL,
    10ULL,
    100ULL,
    1000ULL,
    10000ULL,
    100000ULL,
    1000000ULL,
    10000000ULL,
    100000000ULL,
    1000000000ULL,
    10000000000ULL,
    100000000000ULL,
    1000000000000ULL,
    10000000000000ULL,
    100000000000000ULL,
    1000000000000000ULL,
    10000000000000000ULL,
    100000000000000000ULL,
    1000000000000000000ULL,
};

static llu BINOM[20][20];

// TODO: Precompute this
llu binom(llu n, llu k) {
    if (k > n) {
        return 0;
    }
    if (2 * k > n) {
        return binom(n, n - k);
    }
    if (k == 0) {
        return 1;
    }
    return binom(n - 1, k - 1) * n / k;
}

int play(llu num) {
    while (num >= 10) {
        llu new_num = 1;
        while (num > 0) {
            new_num *= (num % 10);
            num /= 10;
        }
        num = new_num;
    }
    return num;
}

void enumerate(std::array<std::vector<histogram_t>, 10>& result, histogram_t& histogram, int idx, int prev_digit) {
    if (idx == 0) {
        llu num = 0;
        for (int i = 1; i < 10; i++) {
            for (int j = 0; j < histogram[i]; j++) {
                num = 10 * num + (llu)i;
            }
        }
        int play_result = play(num);
        if (play_result != 0) {
            result[play_result].push_back(histogram);
        }
        return;
    }
    for (int d = prev_digit; d < 10; d++) {
        if (d % 2 == 0 && histogram[5] > 0) {
            continue;
        }
        if (d == 5 && (histogram[2] > 0 || histogram[4] > 0)) { 
            continue;
        }

        histogram[d]++;
        enumerate(result, histogram, idx - 1, d);
        histogram[d]--;
    }
}

histogram_t compute(std::array<std::vector<histogram_t>, 10> precomputed, llu upto) {
    histogram_t ret = {{0}};

    char digits[32];
    sprintf(digits, "%llu", upto);
    int len = std::strlen(digits);

    for (int i = 0; i < len; i++) {
        // printf("Position %d (%c)\n", i, digits[i]);
        digits[i] -= '0';

        // For all non-zero digits up to the current one, count the configurations
        // which start with 'd'
        for (int d = 1; d < digits[i]; d++) {
            // printf("Processing digit %d\n", d);
            for (int remainder = 1; remainder < 10; remainder++) {
                const auto& histos = precomputed[remainder];
                for (const histogram_t& histo : histos) {
                    if (histo[d] > 0) {
                        // printf("  Including");
                        // for (int i = 0; i < 10; i++) {
                        //     printf(" %llu", histo[i]);
                        // }
                        // printf("\n");
                        llu counts = 1;
                        llu sum = 0;
                        for (int x = 1; x < 10; x++) {
                            sum += histo[x] - (x == d ? 1 : 0);
                            counts *= BINOM[sum][histo[x] - (x == d ? 1 : 0)];
                        }
                        ret[remainder] += counts;
                    }
                }
            }
        }

        // Filter out the histograms - leave only those which are possible
        // after the current digit is added
        // printf("Continuing to the next digit (%d)\n", digits[i]);
        for (auto& histos : precomputed) {
            int pos = 0;
            while (pos < histos.size()) {
                if (histos[pos][digits[i]] == 0) {
                    // Remove this histogram - doesn't have the necessary digits
                    // printf("  Rejecting");
                    // for (int x = 0; x < 10; x++) {
                    //     printf(" %llu", histos[pos][x]);
                    // }
                    // printf("\n");
                    histos[pos] = histos.back();
                    histos.pop_back();
                } else {
                    // printf("  Keeping  ");
                    // for (int i = 0; i < 10; i++) {
                    //     printf(" %llu", histos[pos][i]);
                    // }
                    // printf("\n");
                    histos[pos][digits[i]]--;
                    pos++;
                }
            }
        }
    }

    // There might be one remaining histogram that we need to include
    for (int i = 1; i < 10; i++) {
        if (!precomputed[i].empty()) {
            // printf("Adding leftover histogram with remainder %d\n", i);
            ret[i]++;
        }
    }

    ret[0] = upto + 1 - POWERS_OF_TEN[len - 1];
    for (int i = 1; i < 10; i++) {
        ret[0] -= ret[i];
    }

    return ret;
};

int main() {
    for (int n = 0; n < 20; n++) {
        for (int k = 0; k < 20; k++) {
            BINOM[n][k] = binom(n, k);
        }
    }

    int t;
    scanf("%d\n", &t);

    std::array<std::array<std::vector<histogram_t>, 10>, 20> precomputed;
    std::array<histogram_t, 20> baselines = {{0}};

    for (int i = 1; i < 20; i++) {
        histogram_t tmp;
        enumerate(precomputed[i], tmp, i, 1);
    }

    llu tenpow = 1;
    for (int i = 1; i < 20; i++) {
        baselines[i] = compute(precomputed[i - 1], tenpow - 1);
        for (int j = 0; j < 10; j++) {
            baselines[i][j] += baselines[i - 1][j];
        }
        tenpow *= 10;
    }

    while (t --> 0) {
        llu n;
        scanf("%llu", &n);

        char digits[32];
        sprintf(digits, "%llu", n);
        int len = std::strlen(digits);

        histogram_t counts = compute(precomputed[len], n);
        for (int i = 0; i < 10; i++) {
            if (i == 9) {
                printf("%llu\n", baselines[len][i] + counts[i]);
            } else {
                printf("%llu ", baselines[len][i] + counts[i]);
            }
        }
    }

    return 0;
}