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
#include <array>
#include <codecvt>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <istream>
#include <ostream>
#include <vector>

using namespace std;

using Sums = std::array<int64_t, 10>;

namespace {
constexpr bool DEBUG = false;

struct Node {
    Sums sums_;
    int digit_;

    Node() : sums_(), digit_(0) { sums_.fill(0); }
    Node(Sums prevSums, int digit) : sums_(prevSums), digit_(digit) { sums_[digit_]++; }
    friend auto operator<<(std::ostream &stream, const Node &node) -> std::ostream & {
        for (int i = 0; i < 10; i++) {
            stream << node.sums_[i] << " ";
        }
        stream << "\n";
        return stream;
    }
};

vector<Node> calculated = {Node()};

auto baseDigit(int64_t number) -> int64_t {
    if (number < calculated.size()) {
        return calculated[number].digit_;
    }
    int64_t multiplication = 1;
    while (number > 0) {
        multiplication *= (number % 10);
        number /= 10;
    }
    return calculated[multiplication].digit_;
}

} // namespace

auto main() -> int {
    std::ios_base::sync_with_stdio(false);

    int t = 0;
    int64_t n = 0;

    cin >> t;
    for (int i = 1; i <= 10; i++) {
        calculated.emplace_back(calculated.back().sums_, i % 10);
    }

    for (int i = 0; i < t; i++) {
        cin >> n;
        for (int64_t j = calculated.size(); j <= n; j++) {
            calculated.emplace_back(calculated.back().sums_, baseDigit(j));
            // cout << j << ":" << baseDigit(j) << " " << calculated[j].sums_[calculated[j].digit_];
        }
        cout << calculated[n];
    }
}