#include <bits/stdc++.h> using namespace std; // #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << endl; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef long long ll; typedef array<ll, 10> arr; typedef pair<ll, ll> pll; namespace ARR { arr add(arr a, arr b) { rep(i, sz(a)) a[i] += b[i]; return a; } ll mult(const arr &a) { ll t = 1; rep(i, sz(a)) rep(_, a[i]) t *= i; return t; } const arr zero = {}; } // namespace ARR #include <bits/extc++.h> /** keep-include */ // To use most bits rather than just the lowest ones: struct chash { // large odd number for C const uint64_t C = ll(4e18 * acos(0)) | 71; ll operator()(ll x) const { return __builtin_bswap64(x * C); } }; template <class V> using ht = __gnu_pbds::gp_hash_table<ll, V, chash>; namespace PRE { const int MAXN = 19; ll fact[MAXN]; ht<pll> cnts[MAXN]; pll policz(const arr &A) { int s = accumulate(all(A), 0ll); ll res = fact[s]; ll den = 1; for (auto a : A) den *= fact[a]; res /= den; auto x = res; x *= (s - A[0]); assert(x % s == 0); x /= s; return {res, x}; } void go() { fact[0] = 1; fwd(i, 1, MAXN) fact[i] = fact[i - 1] * i; // czy druga wartosc ma znaczenie? cnts[0][1] = {1, 0}; vector<int> B(9, 1); fwd(i, 1, MAXN) { auto P = B; P.resize(9 + i); sort(all(P)); arr a = {}; int cnt = 0; do { int j = 0; rep(i, sz(a)) a[i] = 0; rep(i, sz(P)) if (P[i]) j++; else a[j]++; auto m = ARR::mult(a); auto p = policz(a); // cnts[i][m].st += x; cnts[i][m].st += p.st; cnts[i][m].nd += p.nd; cnt++; } while (next_permutation(all(P))); // redo_cnts(i); debug(sz(cnts[i]), cnt); } } } // namespace PRE namespace DIGIT { ht<ll> digit; ll get(ll x) { if (x < 10) return x; auto itr = digit.find(x); if (itr != digit.end()) return itr->nd; ll t = 1; auto cx = x; while (x) { t *= x % 10; x /= 10; } return digit[cx] = get(t); } } // namespace DIGIT namespace HELPER { map<ll, arr> start; arr get_start(ll len) { if (start.contains(len)) return start[len]; arr res = ARR::zero; // disallow 0 fwd(i, 1, len) for (auto [m, v] : PRE::cnts[i]) res[DIGIT::get(m)] += v.nd; return start[len] = res; } map<pll, arr> with_mult; arr get_with_mult(ll len, ll mult) { pll key = {len, mult}; if (with_mult.contains(key)) return with_mult[key]; arr res = ARR::zero; // allow zero for (auto [m, v] : PRE::cnts[len]) res[DIGIT::get(m * mult)] += v.st; return with_mult[key] = res; } } // namespace HELPER arr solve(string t, ll mult = -1) { arr res = ARR::zero; if (sz(t) == 0) { res[DIGIT::get(mult)]++; return res; } if (mult == -1) res = ARR::add(res, HELPER::get_start(sz(t))); int x = t.back() - '0'; t.pop_back(); bool skip_zero = false; if (mult == -1) { skip_zero = true; mult = 1; } for (int i = skip_zero; i < x; i++) res = ARR::add(res, HELPER::get_with_mult(sz(t), mult * i)); return ARR::add(res, solve(t, mult * x)); } void solve() { // mt19937 rng(chrono/::steady_clock::now().time_since_epoch().count()); // int len = uniform_int_distribution<int>(1, 18)(rng); // ll number = uniform_int_distribution<ll>(1, 1e18)(rng); string s; //= to_string(number); // cerr << s << endl; cin >> s; reverse(all(s)); auto res = solve(s); rep(i, sz(res)) cout << res[i] << " "; cout << "\n"; } int32_t main() { _upgrade; PRE::go(); debug("A"); // exit(0); int Z; cin >> Z; while (Z--) { solve(); // cerr << Z << endl; } }
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 | #include <bits/stdc++.h> using namespace std; // #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << endl; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef long long ll; typedef array<ll, 10> arr; typedef pair<ll, ll> pll; namespace ARR { arr add(arr a, arr b) { rep(i, sz(a)) a[i] += b[i]; return a; } ll mult(const arr &a) { ll t = 1; rep(i, sz(a)) rep(_, a[i]) t *= i; return t; } const arr zero = {}; } // namespace ARR #include <bits/extc++.h> /** keep-include */ // To use most bits rather than just the lowest ones: struct chash { // large odd number for C const uint64_t C = ll(4e18 * acos(0)) | 71; ll operator()(ll x) const { return __builtin_bswap64(x * C); } }; template <class V> using ht = __gnu_pbds::gp_hash_table<ll, V, chash>; namespace PRE { const int MAXN = 19; ll fact[MAXN]; ht<pll> cnts[MAXN]; pll policz(const arr &A) { int s = accumulate(all(A), 0ll); ll res = fact[s]; ll den = 1; for (auto a : A) den *= fact[a]; res /= den; auto x = res; x *= (s - A[0]); assert(x % s == 0); x /= s; return {res, x}; } void go() { fact[0] = 1; fwd(i, 1, MAXN) fact[i] = fact[i - 1] * i; // czy druga wartosc ma znaczenie? cnts[0][1] = {1, 0}; vector<int> B(9, 1); fwd(i, 1, MAXN) { auto P = B; P.resize(9 + i); sort(all(P)); arr a = {}; int cnt = 0; do { int j = 0; rep(i, sz(a)) a[i] = 0; rep(i, sz(P)) if (P[i]) j++; else a[j]++; auto m = ARR::mult(a); auto p = policz(a); // cnts[i][m].st += x; cnts[i][m].st += p.st; cnts[i][m].nd += p.nd; cnt++; } while (next_permutation(all(P))); // redo_cnts(i); debug(sz(cnts[i]), cnt); } } } // namespace PRE namespace DIGIT { ht<ll> digit; ll get(ll x) { if (x < 10) return x; auto itr = digit.find(x); if (itr != digit.end()) return itr->nd; ll t = 1; auto cx = x; while (x) { t *= x % 10; x /= 10; } return digit[cx] = get(t); } } // namespace DIGIT namespace HELPER { map<ll, arr> start; arr get_start(ll len) { if (start.contains(len)) return start[len]; arr res = ARR::zero; // disallow 0 fwd(i, 1, len) for (auto [m, v] : PRE::cnts[i]) res[DIGIT::get(m)] += v.nd; return start[len] = res; } map<pll, arr> with_mult; arr get_with_mult(ll len, ll mult) { pll key = {len, mult}; if (with_mult.contains(key)) return with_mult[key]; arr res = ARR::zero; // allow zero for (auto [m, v] : PRE::cnts[len]) res[DIGIT::get(m * mult)] += v.st; return with_mult[key] = res; } } // namespace HELPER arr solve(string t, ll mult = -1) { arr res = ARR::zero; if (sz(t) == 0) { res[DIGIT::get(mult)]++; return res; } if (mult == -1) res = ARR::add(res, HELPER::get_start(sz(t))); int x = t.back() - '0'; t.pop_back(); bool skip_zero = false; if (mult == -1) { skip_zero = true; mult = 1; } for (int i = skip_zero; i < x; i++) res = ARR::add(res, HELPER::get_with_mult(sz(t), mult * i)); return ARR::add(res, solve(t, mult * x)); } void solve() { // mt19937 rng(chrono/::steady_clock::now().time_since_epoch().count()); // int len = uniform_int_distribution<int>(1, 18)(rng); // ll number = uniform_int_distribution<ll>(1, 1e18)(rng); string s; //= to_string(number); // cerr << s << endl; cin >> s; reverse(all(s)); auto res = solve(s); rep(i, sz(res)) cout << res[i] << " "; cout << "\n"; } int32_t main() { _upgrade; PRE::go(); debug("A"); // exit(0); int Z; cin >> Z; while (Z--) { solve(); // cerr << Z << endl; } } |