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
#include <cmath>
#include <cstdio>
#include <vector>
#define DEBUG false
using namespace std;

// TODO: if we nee to optimize, we can create a vector of possible IDs and iterate over it instead of [0, ZERO_ID]

// number is compressed into an ID based on its digit product prime factors.
// all numbers with the same ID will produce the same digit at the end.
// if 2 and 5 are present, the result will always be 0 so they can't be both in there.
const int val_2_5 = 1;
const int val_3 = 55;
const int val_7 = val_3 * 37;
const int val_parity = val_7 * 19; // parity 0 -> 2s only, parity 1 -> 5s only
const int ZERO_ID = val_parity * 2;

int addvalues[10] = {
    ZERO_ID,        // 0
    0,              // 1
    val_2_5,        // 2
    val_3,          // 3
    val_2_5 * 2,    // 4
    val_2_5,        // 5
    val_2_5+val_3,  // 6
    val_7,          // 7
    val_2_5 * 3,    // 8
    val_3 * 2,      // 9
};

long long power10[20] = {
    1LL,                      // 0
    10LL,                     // 1
    100LL,                    // 2
    1000LL,                   // 3
    10000LL,                  // 4
    100000LL,                 // 5
    1000000LL,                // 6
    10000000LL,               // 7
    100000000LL,              // 8
    1000000000LL,             // 9
    10000000000LL,            // 10
    100000000000LL,           // 11
    1000000000000LL,          // 12
    10000000000000LL,         // 13
    100000000000000LL,        // 14
    1000000000000000LL,       // 15
    10000000000000000LL,      // 16
    100000000000000000LL,     // 17
    1000000000000000000LL,    // 18
};

// transitions[X][Y] = id you get by adding digit Y to id X
// some transitions will be nonsense, but it's ok since they never happen
int transitions[ZERO_ID+1][10];

// id_digits[X] = digit you get when converting number of id X
int id_digits[ZERO_ID+1];

int multiply(int id, int next_digit) {
    if (id > ZERO_ID) return ZERO_ID;
    if (id == ZERO_ID || next_digit == 0) return ZERO_ID;
    int new_id = id;
    if (id % val_3 > 0) {
        // it has at least one 2 or 5
        if (id < val_parity && next_digit == 5) {
            // has 2s, and we brought a 5
            return ZERO_ID;
        }
        if (id > val_parity && next_digit % 2 == 0) {
            // has 5s, and we brought a 2/4/8
            return ZERO_ID;
        }
    } else if (next_digit == 5) {
        // first 5; set parity value
        new_id += val_parity;
    }
    new_id += addvalues[next_digit];
    if (new_id > ZERO_ID) return ZERO_ID; // some transitions will be nonsense and its ok
    return new_id;
}

void initialize_transitions() {
    for (int i = 0; i <= ZERO_ID; ++i) {
        for (int d = 0; d <= 9; ++d) {
            transitions[i][d] = multiply(i, d);
        }
    }
}

int end_digit_from_number(long long number) {
    if (number < 10) {
        return static_cast<int>(number);
    }
    long long product = 1;
    while (number > 0) {
        product *= number % 10;
        number /= 10;
    }
    if (product < 10) {
        return static_cast<int>(product);
    }
    return end_digit_from_number(product);
}

int end_digit_from_id(int id) {
    if (id == ZERO_ID) return 0;
    long long product = 1;
    long long d2_5 = 2;
    if (id > val_parity) {
        d2_5 = 5;
        id -= val_parity;
    }
    int num2_5 = id % val_3;
    id /= val_3;
    int num3 = id % 37;
    id /= 37;
    int num7 = id;
    product *= pow(d2_5, num2_5);
    product *= pow(3, num3);
    product *= pow(7, num7);

    return end_digit_from_number(product);
}

void initialize_id_digits() {
    for (int i = 0; i <= ZERO_ID; ++i) {
        id_digits[i] = end_digit_from_id(i);
    }
}

int number_to_id(long long number) {
    if (number == 0) return ZERO_ID;
    int id = 0;
    while (number > 0) {
        id = transitions[id][static_cast<int>(number % 10)];
        number /= 10;
    }
    return id;
}

int get_length(long long number) {
    // this can be faster but do we need it to be tbh
    for (int i = 0; i < 20; ++i) {
        if (number < power10[i]) {
            return i;
        }
    }
    return 19;
}

// full_lenclass_id_count[D][X][Y] = number of numbers (starting with nonzero) of length X of id Y starting with digit lower/equal to D
long long full_lenclass_id_count[10][20][ZERO_ID+1];
// similar to above, but counts numbers of all lengths up to X
long long full_lenclass_upto_id_count[10][20][ZERO_ID+1];

void initialize_full_lenclass_array() {
    // TODO: I think we don't inlude zero here
    for (int i = 1; i < 10; ++i) {
        for (int j = i; j < 10; ++j) {
            full_lenclass_id_count[j][1][number_to_id(i)] = 1;
            full_lenclass_upto_id_count[j][1][number_to_id(i)] = 1;
        }
    }
    for (int l = 2; l <= 19; ++l) {
        for (int i = 1; i < 10; ++i) {
        if (DEBUG) printf("Calcing L = %d,  i = %d\n", l, i);
            for (int id = 0; id <= ZERO_ID; ++id) {
                // l = new number length
                // i = first digit of the new number
                // id = in this iteration we only care about numbers with this id

                // add digit X to any number of lower length
                int new_id = transitions[id][i];
                full_lenclass_id_count[i][l][new_id] += full_lenclass_id_count[9][l-1][id];
                // include previous-digit results
                full_lenclass_id_count[i][l][id] += full_lenclass_id_count[i-1][l][id];
            }
            // add digit X and zeros to any number of length at least 2 lower
            full_lenclass_id_count[i][l][ZERO_ID] += power10[l-2];
        }

        for (int i = 1; i < 10; ++i) {
            for (int id = 0; id <= ZERO_ID; ++id) {
                // same as above but sum Ls
                full_lenclass_upto_id_count[i][l][id] = full_lenclass_id_count[i][l][id] + full_lenclass_upto_id_count[i][l-1][id];
            }
        }
    }
}

void get_digit_counts(long long n) {
    if (DEBUG) printf("============================================\n");
    if (DEBUG) printf("STARTING PROCESS FOR %lld\n", n);
    long long digit_counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    vector<long long> id_results(ZERO_ID+1, 0);
    if (n == 1000'000'000'000'000'000LL) {
        // We can only handle numbers with length of up to 18 digits
        digit_counts[0] += 1;
        n--;
    }

    // compute all IDs of length = L, starting with last digit

    int full_length = get_length(n);
    long long n2 = n;
    int length = 0;
    while (n2 > 0) {
        int last_dig = static_cast<int>(n2 % 10LL);
        n2 /= 10LL;
        length += 1;
        if (DEBUG) printf("Working on digit %d\n", last_dig);
        // add this digit to all previous numbers
        for (int i = ZERO_ID; i >= 0; --i) {
            long long old_ids = id_results[i];
            id_results[i] = 0;
            id_results[transitions[i][last_dig]] += old_ids;
        }
        if (DEBUG) printf("Added all prevnumbers multed\n");
        // add numbers attainable with lower digits (add [<d][any l-1])
        if (last_dig > 1) {
            if (DEBUG) printf("Zero params: %d %d\n", last_dig-1, length);
            if (DEBUG) printf("Adding %lld zeros through dig-1\n", full_lenclass_id_count[last_dig - 1][length][ZERO_ID]);
            for (int i = ZERO_ID; i >= 0; --i) {
                id_results[i] += full_lenclass_id_count[last_dig - 1][length][i];
            }
        }
        if (DEBUG) printf("Added all lowerdigits\n");
        // add zeros for next dig
        if (last_dig > 0 && n2 > 0) {
            if (DEBUG) printf("Adding %lld zeros\n", power10[length-1]);
            id_results[ZERO_ID] += power10[length-1];
        }
        if (DEBUG) printf("Added zeros\n");
    }

    // HERE WE HAVE ALL NUMBERS OF LENGTH EQUAL TO LEN(N)

    if (full_length > 1) {
        for (int i = ZERO_ID; i >= 0; --i) {
            id_results[i] += full_lenclass_upto_id_count[9][full_length-1][i];
            digit_counts[id_digits[i]] += id_results[i];
        }
    }

    // not sure why its not counted but lets go
    int maxdig = id_digits[number_to_id(n)];
    digit_counts[maxdig]++;

    // PRINT THE DIGITS

    for (int i = 0; i < 10; ++i) {
        printf("%lld ", digit_counts[i]);
    }
    printf("\n");

}

int main() {
    if (DEBUG) printf("transition init\n");
    initialize_transitions();
    if (DEBUG) printf("id digit init\n");
    initialize_id_digits();
    if (DEBUG) printf("full_lenclass init\n");
    initialize_full_lenclass_array();
    if (DEBUG) printf("init done\n");

    int t;
    scanf("%d", &t);
    for (int t_i = 0; t_i < t; ++t_i) {
        long long n;
        scanf("%lld", &n);

        get_digit_counts(n);
    }

    return 0;
}