#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
#define pb push_back
#define mp make_pair
#define x first
#define y second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x) for (auto& a : (x))
#define all(x) (x).begin(), (x).end()
#define sz(x) int((x).size())
void run();
int main() {
cin.sync_with_stdio(0); cin.tie(0);
cout << fixed << setprecision(12);
run();
cout << flush; _Exit(0);
}
struct Key {
uint8_t &unk() { return cnt[0]; }
uint8_t &operator[](int i) {
assert(2 <= i && i <= 9);
return cnt[i-1];
}
uint64_t pack() {
uint64_t ret = 0;
each(v, cnt) ret = (ret<<5) | v;
return ret;
}
uint8_t cnt[9] = {};
};
using Counts = array<ll, 10>;
vector<uint64_t> keys;
vector<Counts> values;
ll ten[19];
Counts& operator+=(Counts& lhs, const Counts& rhs) {
rep(i, 0, 10) lhs[i] += rhs[i];
return lhs;
}
int simulate(ll val) {
while (val >= 10) {
ll nxt = 1;
while (val > 0) {
nxt *= val%10;
val /= 10;
}
val = nxt;
}
return int(val);
}
int simulate(Key key) {
assert(key.unk() == 0);
ll val = 0;
rep(d, 2, 10) rep(i, 0, key[d]) val = val*10 + d;
return val > 0 ? simulate(val) : 1;
}
void iterKeys(Key& key, int rem, int i, auto& fn) {
if (i >= 9) return fn(key);
uint8_t& cur = key.cnt[i];
for (cur = 0; cur <= rem; cur++) {
iterKeys(key, rem-cur, i+1, fn);
}
}
void iterKeys(auto fn) {
Key key;
iterKeys(key, 18, 0, fn);
}
Counts& lookup(Key key) {
uint64_t x = key.pack();
auto it = lower_bound(all(keys), x);
assert(it != keys.end() && *it == x);
return *(it - keys.begin() + values.begin());
}
void precompute() {
iterKeys([&](Key key) { keys.pb(key.pack()); });
sort(all(keys));
values.resize(sz(keys));
ten[0] = 1;
rep(i, 1, 19) ten[i] = ten[i-1] * 10;
iterKeys([&](Key key) {
auto& out = lookup(key);
if (key.unk() == 0) {
out[simulate(key)] = 1;
return;
}
key.unk()--;
out = lookup(key); // 1
out[0] += ten[key.unk()]; // 0
rep(d, 2, 10) {
key[d]++;
out += lookup(key);
key[d]--;
}
});
}
struct KeyExt {
Key key;
uint8_t special[2] = {};
uint8_t &unk() { return key.unk(); }
uint8_t &operator[](int i) { return i < 2 ? special[i] : key[i]; }
};
Counts lookup(KeyExt ext) {
if (ext.special[0] > 0) {
Counts ret = {};
ret[0] = ten[ext.key.unk()];
return ret;
}
return lookup(ext.key);
}
void solve() {
string n; cin >> n;
Counts ans = {};
// shorter than n
rep(len, 1, sz(n)) rep(d, 1, 10) {
KeyExt key;
key[d] = 1;
key.unk() = int8_t(len-1);
ans += lookup(key);
}
// first digit lower
rep(d, 1, n[0]-'0') {
KeyExt key;
key[d] = 1;
key.unk() = int8_t(sz(n)-1);
ans += lookup(key);
}
KeyExt key;
key[n[0]-'0'] = 1;
// i-th digit lower
rep(i, 1, sz(n)) {
key.unk() = int8_t(sz(n)-i-1);
rep(d, 0, n[i]-'0') {
key[d]++;
ans += lookup(key);
key[d]--;
}
key[n[i]-'0']++;
}
// equal to n
key.unk() = 0;
ans += lookup(key);
each(v, ans) cout << v << ' ';
cout << '\n';
}
void run() {
precompute();
int t; cin >> t;
while (t--) {
solve();
}
}
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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define deb(...) #define DBP(...) using namespace std; using ll = long long; using vi = vector<int>; using pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) void run(); int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); run(); cout << flush; _Exit(0); } struct Key { uint8_t &unk() { return cnt[0]; } uint8_t &operator[](int i) { assert(2 <= i && i <= 9); return cnt[i-1]; } uint64_t pack() { uint64_t ret = 0; each(v, cnt) ret = (ret<<5) | v; return ret; } uint8_t cnt[9] = {}; }; using Counts = array<ll, 10>; vector<uint64_t> keys; vector<Counts> values; ll ten[19]; Counts& operator+=(Counts& lhs, const Counts& rhs) { rep(i, 0, 10) lhs[i] += rhs[i]; return lhs; } int simulate(ll val) { while (val >= 10) { ll nxt = 1; while (val > 0) { nxt *= val%10; val /= 10; } val = nxt; } return int(val); } int simulate(Key key) { assert(key.unk() == 0); ll val = 0; rep(d, 2, 10) rep(i, 0, key[d]) val = val*10 + d; return val > 0 ? simulate(val) : 1; } void iterKeys(Key& key, int rem, int i, auto& fn) { if (i >= 9) return fn(key); uint8_t& cur = key.cnt[i]; for (cur = 0; cur <= rem; cur++) { iterKeys(key, rem-cur, i+1, fn); } } void iterKeys(auto fn) { Key key; iterKeys(key, 18, 0, fn); } Counts& lookup(Key key) { uint64_t x = key.pack(); auto it = lower_bound(all(keys), x); assert(it != keys.end() && *it == x); return *(it - keys.begin() + values.begin()); } void precompute() { iterKeys([&](Key key) { keys.pb(key.pack()); }); sort(all(keys)); values.resize(sz(keys)); ten[0] = 1; rep(i, 1, 19) ten[i] = ten[i-1] * 10; iterKeys([&](Key key) { auto& out = lookup(key); if (key.unk() == 0) { out[simulate(key)] = 1; return; } key.unk()--; out = lookup(key); // 1 out[0] += ten[key.unk()]; // 0 rep(d, 2, 10) { key[d]++; out += lookup(key); key[d]--; } }); } struct KeyExt { Key key; uint8_t special[2] = {}; uint8_t &unk() { return key.unk(); } uint8_t &operator[](int i) { return i < 2 ? special[i] : key[i]; } }; Counts lookup(KeyExt ext) { if (ext.special[0] > 0) { Counts ret = {}; ret[0] = ten[ext.key.unk()]; return ret; } return lookup(ext.key); } void solve() { string n; cin >> n; Counts ans = {}; // shorter than n rep(len, 1, sz(n)) rep(d, 1, 10) { KeyExt key; key[d] = 1; key.unk() = int8_t(len-1); ans += lookup(key); } // first digit lower rep(d, 1, n[0]-'0') { KeyExt key; key[d] = 1; key.unk() = int8_t(sz(n)-1); ans += lookup(key); } KeyExt key; key[n[0]-'0'] = 1; // i-th digit lower rep(i, 1, sz(n)) { key.unk() = int8_t(sz(n)-i-1); rep(d, 0, n[i]-'0') { key[d]++; ans += lookup(key); key[d]--; } key[n[i]-'0']++; } // equal to n key.unk() = 0; ans += lookup(key); each(v, ans) cout << v << ' '; cout << '\n'; } void run() { precompute(); int t; cin >> t; while (t--) { solve(); } } |
English