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
#include <bits/stdc++.h>
#define int long long
#define pii pair<int, int>
#define piii pair<pair<int, int>, int>
#define st first.first
#define nd first.second
#define rd second
#define For(i, l, r) for (int i = l; i <= r; i++)
#define Forcin(l, r, a)          \
    for (int i = l; i <= r; i++) \
        cin >> a[i];
#define Ford(i, l, r) for (int i = l; i >= r; i--)
#define ben(v) v.begin(), v.end()
#define siz(v) (int)v.size()
#define LOCAL 0
#define LOCAL2 0
using namespace std;

const int M = 10'000'005, inf=1e15, L=1005;
int t, a[L], fact[25];
int res[L][10];
char dp[M];

inline int f(int x)
{
    int ret = 1;
    while (x > 0)
    {
        ret *= (x % 10);
        x /= 10;
    }
    return ret;
}

int f2(int x)
{
    if (x<10)
        return x;
    if (x<M)
        return dp[x];
    return f2(f(x));
}

int n_permutations_no_zero(vector<char> &s)
{
    int n = s.size();
    int c[10];
    For(i, 1, 9)
        c[i] = 0;
    for (auto el : s)
        c[el]++;
    int ret = fact[n];
    For(i, 1, 9)
        ret /= fact[c[i]];
    return ret;
}

inline int n_permutations(vector <char> &c){
    int n=0;
    For(i, 1, 9)
        n+=c[i];
    int ret=fact[n];
    For(i, 1, 9)
        ret/=fact[c[i]];
    return ret;
}

void add_to_res(vector <char> s){
    if (siz(s) == 0 || s.back()==0){
        return;
    }
    int n=s.size();
    int c[10];
    For(i, 1, 9)
        c[i]=0;
    for(auto el:s)
        c[el]++;
    int mini=0;
    For(i, 0, n-1){
        mini*=10;
        mini+=s[i];
    }
    int maxi=0;
    Ford(i, n-1, 0){
        maxi*=10;
        maxi+=s[i];
    }
    int final=f2(mini), full=n_permutations_no_zero(s);
    if (final==0)
        return;
    For(i, 1, t){
        int x=a[i];
        if (maxi<=x){
            res[i][final]+=full;
        }
        else if (mini<=x){
            unsigned int mn=1;
            while(mn<x)
                mn*=10;
            mn/=10;
            char u[10];
            u[0]=0;
            For(j, 1, 9)
                u[j]=c[j];
            int cur=full, rem=n;
            For(p, 1, n){
                int d=(x/mn)%10;
                int cur_sum=0;
                For(cyf, 1, d-1){
                    if (u[cyf]==0)
                        continue;
                    cur_sum+=(cur*u[cyf]);
                }
                res[i][final]+=(cur_sum/rem);
                mn/=10;
                if (u[d]==0)
                    break;
                cur*=u[d];
                cur/=rem;
                u[d]--;
                rem--;
                if (p==n)
                    res[i][final]++;
            }
        }
    }
}

void subsets(int cur_dig, int rem, vector<char> &s){
    if (cur_dig>=10 || rem==0){
        add_to_res(s);
        return;
    }
    For(i, 0, rem){
        subsets(cur_dig+1, rem-i, s);
        s.push_back(cur_dig);
    }
    while(s.size()>0&&s.back()==cur_dig)
        s.pop_back();
}

signed main()
{
    cin.tie(0)->sync_with_stdio();
    if (LOCAL)
        freopen("MNO/small/1.in", "r", stdin);
    if (LOCAL2)
        freopen("local_out.txt", "w", stdout);
    int maxi=0;
    cin >> t;
    For(i, 1, t)
    {
        cin >> a[i];
        if (a[i]%10==0){
            a[i]--;
            res[i][0]++;
        }
        maxi=max(a[i], maxi);
    }
    For(i, 0, 9)
        dp[i] = i;
    For(i, 10, M-1)
        dp[i] = dp[f(i)];
    fact[0]=1;
    For(i, 1, 20){
        fact[i]=fact[i-1]*i;
    }
    int maxd=0;
    while(maxi>0){
        maxd++;
        maxi/=10;
    }
    vector <char> v;
    subsets(1, maxd, v);
    For(i, 1, t){
        int sum=0;
        For(j, 1, 9)
            sum+=res[i][j];
        res[i][0]+=(a[i]-sum);
        For(j, 0, 9){
            cout << res[i][j]<<' ';
        }
        cout << '\n';
    }
    /* For(j, 1, t){
        int sum=0;
        For(i, 0, 9)
            sum+=res[j][i];
        if (sum!=a[j]){
            cout << a[j]<<' '<<sum;
            break;
        }
    } */
}