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
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<(int)b;++i)
#define FORD(i,a,b) for(int i=a;i>=(int)b;--i)
#define PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

template <class T>
istream& operator>> (istream& is, vector<T>& vec){
    FOR(i,0,vec.size()) is >> vec[i];
    return is;
}
template <class T>
ostream& operator<< (ostream& os, vector<T>& vec){
    for(auto& t : vec) os << t << " ";
    return os;
}
template<class T, class U>
ostream& operator<< (ostream& os, const pair<T, U>& p){
    os << p.FI << " " << p.SE;
    return os;
}
template<class T, class U>
istream& operator>> (istream& is, pair<T, U>& p){
    is >> p.FI >> p.SE;
    return is;
}

const int N = 18;

int digprod(ll n) {
    if (n < 10) return n;
    ll prod = 1;
    while(n > 0) {
        prod *= (n % 10);
        n /= 10;
    }
    return digprod(prod);
}

template<class GDP>
void assure_any(vector<vvll>& any, const vector<vector<pll>>& dystrybucja, int i, int j, GDP& get_dp, vll& dpsz){
    if(any[i][j][0] != -1) return;

    FOR(k,0,10) any[i][j][k] = 0;
    for(auto& [x, y] : dystrybucja[j]) {
        ll p = x * dpsz[i];
        any[i][j][get_dp(p)] += y;
    }
}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    vll dpsz;
    dpsz.PB(0);
    dpsz.PB(1);
    FOR(i,0,N){
        int c = dpsz.size();
        FOR(k,1,10) FOR(p,0,c) dpsz.PB(dpsz[p] * k);
        sort(ALL(dpsz));
        dpsz.erase(unique(ALL(dpsz)), dpsz.end());
    }
    vi dp(dpsz.size());
    FOR(i,0,dp.size()) dp[i] = digprod(dpsz[i]);

    auto get_idx = [&dpsz](ll x) { return lower_bound(ALL(dpsz), x) - dpsz.begin(); };
    auto get_dp = [&dpsz, &dp](ll x) { return dp[lower_bound(ALL(dpsz), x) - dpsz.begin()]; };

    vector<vector<pll>> dystrybucja(N);
    vector<vvll> any(dp.size(), vvll(N, vll(10, -1)));

    dystrybucja[0].EB(1, 1);
    FOR(i,1,N){
        map<ll, ll> to_cast;
        FOR(d,0,10) {
            for(auto& [a, b] : dystrybucja[i - 1]) {
                ll v = a * d;
                to_cast[v] += b;
                FOR(j,1,N-i+1) any[get_idx(v)][j][0] = -1;
            }
        }
        for(auto& p : to_cast) dystrybucja[i].PB(p);
    }

    vvll prep(N, vll(10, 0));
    ll nadprod = 1;
    FOR(i,1,N) {
        for(auto& [x, y] : dystrybucja[i]) prep[i][get_dp(x)] += y;
        prep[i][0] -= nadprod;
        nadprod *= 10;
        FOR(j,0,10) prep[i][j] += prep[i - 1][j];
    }

    FOR(i,0,dp.size()) FOR(j,0,10) any[i][0][j] = (j == dp[i]);

    /*
    FOR(i,0,dp.size()) FOR(j,1,N) if (any[i][j][0] == -1) {
        any[i][j][0] = 0;
        for(auto& [x, y] : dystrybucja[j]) {
            ll p = x * dpsz[i];
            any[i][j][get_dp(p)] += y;
        }
    }
    */

    int t;
    cin >> t;
    while(t--){
        ll tn;
        cin >> tn;
        ll n = tn;
        if(n == 1000000000000000000LL) n--;
        vi digs;
        while(n > 0) {
            digs.PB(n % 10);
            n /= 10;
        }
        reverse(ALL(digs));
        vll ans(prep[digs.size() - 1]);
        ans[digprod(tn)]++;
        if(tn == 1000000000000000000LL) ans[0]++;
        ll cp = 1;
        FOR(i,0,digs.size()) {
            FOR(k,(i == 0),digs[i]){
                ll tcp = cp * k;
                int tcp_idx = get_idx(tcp);
                int stars = digs.size() - i - 1;
                assure_any(any, dystrybucja, tcp_idx, stars, get_dp, dpsz);
                auto& ddd = any[tcp_idx][stars];
                FOR(c,0,10) ans[c] += ddd[c];
            }
            cp *= digs[i];
        }
        cout << ans << endl;
    }

}