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
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

//#pragma GCC optimize("O0")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
#pragma GCC optimize("unroll-loops")

using namespace std;
//using namespace __gnu_pbds;

#define pb push_back
#define F first
#define S second
#define ll long long
#define ld double long
#define ull unsigned long long
//#define endl '\n'


const ll N = 5e4 + 36;
const ll M = 1e2 + 36;
const ll INF = 1e9 + 7;
const ll MOD = 1e9 + 7;
const ll MOD1 = 888888901;
const ll MOD2 = 999988901;
const ll X[8] = {1, -1, 2, 2, -2, -2, 1, -1};
const ll Y[8] = {2, 2, 1, -1, 1, -1, -2, -2};
const ld PI = 3.14159265358979323846;
const ld EPS = 1e-10;

//tree < pair < string, int >, null_type, less < pair < string, int > >, rb_tree_tag, tree_order_statistics_node_update > a;

//mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
mt19937 gen(19937);

ll soch[M][M];

inline ll CC(ll x, int n, int k) {
    if (n - k >= 0 && k >= 1) {
        return x / soch[n - k][k] * soch[n - k][k - 1];
    } else {
        return 0;
    }
}

inline ll binpow (ll a, ll n) {
    ll res = 1;
    while (n) {
        if (n & 1)
            res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}
inline void dfs(vector<ll>& kol,
         vector<pair<string, vector<ll>>>& ans,
         int k,
         int pos,
         ll cnt,
         ll x = 1) {

    if (kol[pos - 1]) {
        ll res = x;

        while (res > 9) {
            ll y = res;
            ll nw = 1;
            while (y) {
                nw *= (y % 10);
                y /= 10;
            }
            res = nw;
        }

        if (res != 0) {
            for (auto& now : ans) {
                if (k == now.F.size()) {
                    auto tmp_kol = kol;
                    ll cnt_now = cnt, tmp = cnt;
                    int kk = k;

                    for (auto sym : now.F) {
                        auto v = int(sym - '0');
                        for (int j(9); j > v; --j) {
                            cnt_now -= CC(tmp, kk, tmp_kol[j]);
                        }
                        if (tmp_kol[v]) {
                            tmp = CC(tmp, kk, tmp_kol[v]);
                            --kk;
                            --tmp_kol[v];
                        } else {
                            break;
                        }
                    }

                    now.S[res] += cnt_now;
                } else if (k < now.F.size()) {
                    now.S[res] += cnt;
                }
            }
        }
    }

    if (pos == 10) {
        return;
    }
    for (ll j(0), dx = 1; j + k <= 18; ++j, dx *= pos) {
        kol[pos] += j;
        if (!(kol[2] && kol[5])) {
            dfs(kol,
                ans,
                k + j,
                pos + 1,
                cnt * soch[k][j],
                x * dx);
        }
        kol[pos] -= j;
    }

}

signed main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif // LOCAL
    for (int i(0); i < 100; ++i) {
        soch[0][i] = 1;
        soch[i][0] = 1;
    }

    for (int i(1); i < 100; ++i) {
        for (int j(1); j < 100; ++j) {
            soch[i][j] = soch[i - 1][j] + soch[i][j - 1];
        }
    }

    int n;
    cin >> n;
    vector<ll> a(n);
    vector<pair<string, vector<ll>>> ans;
    for (int i(0); i < n; ++i) {
        cin >> a[i];
        ans.pb({to_string(a[i]), {}});
        ans.back().S.resize(10);
    }

    vector<ll> kol(10);
    dfs(kol, ans, 0, 1, 1);

    for (int i = 0; i < n; ++i) {
        ans[i].S[0] = a[i];
        for (int j = 1; j < 10; ++j) {
            ans[i].S[0] -= ans[i].S[j];
        }
    }

    for (auto& x : ans) {
        for (auto y : x.S) {
            cout << y << ' ';
        }
        cout << endl;
    }
    return 0;
}