#include "bits/stdc++.h" using namespace std; using ll = long long; ll f(ll n) { while (n >= 10) { ll nxt = 1; while (n) { nxt *= (n % 10); n /= 10; } n = nxt; } return n; } set <ll> all_numbers; vector <ll> primes = {2, 3, 5, 7}; void generate(ll cur, int index) { if (cur <= 1'000'000'000'000'000'000) { if (index == primes.size()) return; all_numbers.insert(cur); generate(cur * primes[index], index); generate(cur, index + 1); } } ll silnia[20], ile[10]; void factorize(ll n) { for (int i = 1; i <= 9; i++) ile[i] = 0; for (int i = 0; i < primes.size(); i++) { while (n % primes[i] == 0) { ile[primes[i]]++; n /= primes[i]; } } } static unordered_map<unsigned long long, ll> permCache; unsigned long long encode(const array<ll,10> &v) { unsigned long long key = 0, base = 1; for (int d = 1; d <= 9; d++) { key += v[d] * base; base *= 21; } return key; } ll all_perm_cached(const array<ll,10>& v) { unsigned long long key = encode(v); auto it = permCache.find(key); if(it != permCache.end()) return it->second; int total = 0; for (int d = 1; d <= 9; d++) total += v[d]; ll res = silnia[total]; for (int d = 1; d <= 9; d++) res /= silnia[v[d]]; permCache[key] = res; return res; } ll all_perm(array <ll, 10> &v) { return silnia[v[1] + v[2] + v[3] + v[4] + v[5] + v[6] + v[7] + v[8] + v[9]] / silnia[v[1]] / silnia[v[2]] / silnia[v[3]] / silnia[v[4]] / silnia[v[5]] / silnia[v[6]] / silnia[v[7]] / silnia[v[8]] / silnia[v[9]]; } vector <ll> N; ll ans[10]; void count(array <ll, 10> v, int cnt, int g) { if (cnt > N.size()) return; while (cnt < N.size()) { ans[g] += all_perm_cached(v); v[1]++; cnt++; } for (int i = 0; i < N.size(); i++) { for (int d = 1; d < N[i]; d++) { if (v[d]) { v[d]--; ans[g] += all_perm_cached(v); v[d]++; } } if (v[N[i]]) v[N[i]]--; else break; if (i == N.size() - 1) ans[g]++; } } vector <vector <array <ll, 10>>> allsets(10); void all_sets(int g, int idx, int cnt) { if (idx == 4) { if (cnt <= 18) { array <ll, 10> v; for (int i = 0; i < 10; i++) v[i] = ile[i]; allsets[g].push_back(v); } return; } if (idx == 0) { all_sets(g, idx + 1, cnt); if (ile[2] >= 2) { ile[2] -= 2; ile[4] += 1; all_sets(g, idx, cnt - 1); ile[4] -= 1; ile[2] += 2; } } else if (idx == 1) { all_sets(g, idx + 1, cnt); if (ile[2] >= 3) { ile[2] -= 3; ile[8] += 1; all_sets(g, idx, cnt - 2); ile[8] -= 1; ile[2] += 3; } } else if (idx == 2) { all_sets(g, idx + 1, cnt); if (ile[2] >= 1 && ile[3] >= 1) { ile[2] -= 1; ile[3] -= 1; ile[6] += 1; all_sets(g, idx, cnt - 1); ile[6] -= 1; ile[3] += 1; ile[2] += 1; } } else if (idx == 3 && cnt - ile[3] / 2 <= 18) { all_sets(g, idx + 1, cnt); if (ile[3] >= 2) { ile[3] -= 2; ile[9] += 1; all_sets(g, idx, cnt - 1); ile[9] -= 1; ile[3] += 2; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); silnia[0] = 1; for (int i = 1; i < 20; i++) { silnia[i] = silnia[i - 1] * i; } generate(1, 0); for (auto x : all_numbers) { ll g = f(x); if (g == 0) continue; factorize(x); if (x == 1) ile[1]++; ll cnt = 0; for (int j = 1; j <= 9; j++) cnt += ile[j]; all_sets(g, 0, cnt); } vector <ll> sizes(all_numbers.size(), 0); int idx = 0; for (int i = 1; i <= 9; i++) { for (auto &a: allsets[i]) { sizes[idx++] = accumulate(a.begin(), a.end(), 0LL); } } int t; cin >> t; while (t--) { for (int i = 0; i < 10; i++) ans[i] = 0; ll xd; cin >> xd; ll zero = xd; N.clear(); while (xd) { N.push_back(xd % 10); xd /= 10; } reverse(N.begin(), N.end()); for (int i = 1, idx = 0; i <= 9; i++) { for (auto &a : allsets[i]) { count(a, sizes[idx++], i); } } for (int i = 1; i < 10; i++) zero -= ans[i]; cout << zero << ' '; for (int i = 1; i < 10; i++) cout << ans[i] << ' '; cout << '\n'; } }
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 | #include "bits/stdc++.h" using namespace std; using ll = long long; ll f(ll n) { while (n >= 10) { ll nxt = 1; while (n) { nxt *= (n % 10); n /= 10; } n = nxt; } return n; } set <ll> all_numbers; vector <ll> primes = {2, 3, 5, 7}; void generate(ll cur, int index) { if (cur <= 1'000'000'000'000'000'000) { if (index == primes.size()) return; all_numbers.insert(cur); generate(cur * primes[index], index); generate(cur, index + 1); } } ll silnia[20], ile[10]; void factorize(ll n) { for (int i = 1; i <= 9; i++) ile[i] = 0; for (int i = 0; i < primes.size(); i++) { while (n % primes[i] == 0) { ile[primes[i]]++; n /= primes[i]; } } } static unordered_map<unsigned long long, ll> permCache; unsigned long long encode(const array<ll,10> &v) { unsigned long long key = 0, base = 1; for (int d = 1; d <= 9; d++) { key += v[d] * base; base *= 21; } return key; } ll all_perm_cached(const array<ll,10>& v) { unsigned long long key = encode(v); auto it = permCache.find(key); if(it != permCache.end()) return it->second; int total = 0; for (int d = 1; d <= 9; d++) total += v[d]; ll res = silnia[total]; for (int d = 1; d <= 9; d++) res /= silnia[v[d]]; permCache[key] = res; return res; } ll all_perm(array <ll, 10> &v) { return silnia[v[1] + v[2] + v[3] + v[4] + v[5] + v[6] + v[7] + v[8] + v[9]] / silnia[v[1]] / silnia[v[2]] / silnia[v[3]] / silnia[v[4]] / silnia[v[5]] / silnia[v[6]] / silnia[v[7]] / silnia[v[8]] / silnia[v[9]]; } vector <ll> N; ll ans[10]; void count(array <ll, 10> v, int cnt, int g) { if (cnt > N.size()) return; while (cnt < N.size()) { ans[g] += all_perm_cached(v); v[1]++; cnt++; } for (int i = 0; i < N.size(); i++) { for (int d = 1; d < N[i]; d++) { if (v[d]) { v[d]--; ans[g] += all_perm_cached(v); v[d]++; } } if (v[N[i]]) v[N[i]]--; else break; if (i == N.size() - 1) ans[g]++; } } vector <vector <array <ll, 10>>> allsets(10); void all_sets(int g, int idx, int cnt) { if (idx == 4) { if (cnt <= 18) { array <ll, 10> v; for (int i = 0; i < 10; i++) v[i] = ile[i]; allsets[g].push_back(v); } return; } if (idx == 0) { all_sets(g, idx + 1, cnt); if (ile[2] >= 2) { ile[2] -= 2; ile[4] += 1; all_sets(g, idx, cnt - 1); ile[4] -= 1; ile[2] += 2; } } else if (idx == 1) { all_sets(g, idx + 1, cnt); if (ile[2] >= 3) { ile[2] -= 3; ile[8] += 1; all_sets(g, idx, cnt - 2); ile[8] -= 1; ile[2] += 3; } } else if (idx == 2) { all_sets(g, idx + 1, cnt); if (ile[2] >= 1 && ile[3] >= 1) { ile[2] -= 1; ile[3] -= 1; ile[6] += 1; all_sets(g, idx, cnt - 1); ile[6] -= 1; ile[3] += 1; ile[2] += 1; } } else if (idx == 3 && cnt - ile[3] / 2 <= 18) { all_sets(g, idx + 1, cnt); if (ile[3] >= 2) { ile[3] -= 2; ile[9] += 1; all_sets(g, idx, cnt - 1); ile[9] -= 1; ile[3] += 2; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); silnia[0] = 1; for (int i = 1; i < 20; i++) { silnia[i] = silnia[i - 1] * i; } generate(1, 0); for (auto x : all_numbers) { ll g = f(x); if (g == 0) continue; factorize(x); if (x == 1) ile[1]++; ll cnt = 0; for (int j = 1; j <= 9; j++) cnt += ile[j]; all_sets(g, 0, cnt); } vector <ll> sizes(all_numbers.size(), 0); int idx = 0; for (int i = 1; i <= 9; i++) { for (auto &a: allsets[i]) { sizes[idx++] = accumulate(a.begin(), a.end(), 0LL); } } int t; cin >> t; while (t--) { for (int i = 0; i < 10; i++) ans[i] = 0; ll xd; cin >> xd; ll zero = xd; N.clear(); while (xd) { N.push_back(xd % 10); xd /= 10; } reverse(N.begin(), N.end()); for (int i = 1, idx = 0; i <= 9; i++) { for (auto &a : allsets[i]) { count(a, sizes[idx++], i); } } for (int i = 1; i < 10; i++) zero -= ans[i]; cout << zero << ' '; for (int i = 1; i < 10; i++) cout << ans[i] << ' '; cout << '\n'; } } |