#include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr i64 inf = 1E18; int f(i64 n) { while (n >= 10) { std::string s = std::to_string(n); i64 res = 1; for (auto c : s) { res *= c - '0'; } n = res; } return n; } std::map<i64, int> prods; std::vector<i64> v; void dfs(int x, int cnt, i64 prod) { if (x > 9) { if (!prods.contains(prod)) { v.push_back(prod); prods[prod] = prods.size(); } return; } bool ok = false; for (int i = 0; i + cnt <= 18; i++) { if (i) { prod *= x; } dfs(x + 1, cnt + i, prod); } } int tot; std::vector<std::array<int, 10>> nxt; std::vector<int> final; int start[10]; std::vector<i64> tail[19][10]; void solve() { i64 n; std::cin >> n; std::vector<std::array<i64, 2>> dp(tot); n++; std::string s = std::to_string(n); std::array<i64, 10> ans {}; int cur = start[1]; for (int i = 0; i < s.size() && cur != -1; i++) { for (int x = 1; x < s[i] - '0'; x++) { for (int y = 1; y <= 9; y++) { if (nxt[cur][x] != -1) { ans[y] += tail[s.size() - 1 - i][y][nxt[cur][x]]; } } } cur = nxt[cur][s[i] - '0']; } for (int i = 1; i < s.size(); i++) { for (int x = 1; x <= 9; x++) { ans[x] += tail[i][x][start[1]]; } } ans[0] = n - 1; for (int i = 1; i <= 9; i++) { ans[0] -= ans[i]; } for (int i = 0; i < 10; i++) { std::cout << ans[i] << " \n"[i == 9]; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); dfs(1, 0, 1); for (int i = 1; i <= 9; i++) { start[i] = prods[i]; } tot = prods.size(); nxt.resize(tot); final.resize(tot); for (int i = 0; i < tot; i++) { final[i] = f(v[i]); for (int j = 0; j < 10; j++) { i64 nv = v[i] * j; if (prods.contains(nv)) { nxt[i][j] = prods[nv]; } else { nxt[i][j] = -1; } } } for (int n = 0; n <= 18; n++) { for (int x = 1; x <= 9; x++) { tail[n][x].resize(tot); for (int i = 0; i < tot; i++) { if (n == 0) { tail[n][x][i] += (final[i] == x); } else { for (int d = 1; d <= 9; d++) { if (nxt[i][d] != -1) { tail[n][x][i] += tail[n - 1][x][nxt[i][d]]; } } } } } } int t; std::cin >> t; while (t--) { solve(); } return 0; }
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 | #include <bits/stdc++.h> using i64 = long long; using u64 = unsigned long long; using u32 = unsigned; using u128 = unsigned __int128; constexpr i64 inf = 1E18; int f(i64 n) { while (n >= 10) { std::string s = std::to_string(n); i64 res = 1; for (auto c : s) { res *= c - '0'; } n = res; } return n; } std::map<i64, int> prods; std::vector<i64> v; void dfs(int x, int cnt, i64 prod) { if (x > 9) { if (!prods.contains(prod)) { v.push_back(prod); prods[prod] = prods.size(); } return; } bool ok = false; for (int i = 0; i + cnt <= 18; i++) { if (i) { prod *= x; } dfs(x + 1, cnt + i, prod); } } int tot; std::vector<std::array<int, 10>> nxt; std::vector<int> final; int start[10]; std::vector<i64> tail[19][10]; void solve() { i64 n; std::cin >> n; std::vector<std::array<i64, 2>> dp(tot); n++; std::string s = std::to_string(n); std::array<i64, 10> ans {}; int cur = start[1]; for (int i = 0; i < s.size() && cur != -1; i++) { for (int x = 1; x < s[i] - '0'; x++) { for (int y = 1; y <= 9; y++) { if (nxt[cur][x] != -1) { ans[y] += tail[s.size() - 1 - i][y][nxt[cur][x]]; } } } cur = nxt[cur][s[i] - '0']; } for (int i = 1; i < s.size(); i++) { for (int x = 1; x <= 9; x++) { ans[x] += tail[i][x][start[1]]; } } ans[0] = n - 1; for (int i = 1; i <= 9; i++) { ans[0] -= ans[i]; } for (int i = 0; i < 10; i++) { std::cout << ans[i] << " \n"[i == 9]; } } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); dfs(1, 0, 1); for (int i = 1; i <= 9; i++) { start[i] = prods[i]; } tot = prods.size(); nxt.resize(tot); final.resize(tot); for (int i = 0; i < tot; i++) { final[i] = f(v[i]); for (int j = 0; j < 10; j++) { i64 nv = v[i] * j; if (prods.contains(nv)) { nxt[i][j] = prods[nv]; } else { nxt[i][j] = -1; } } } for (int n = 0; n <= 18; n++) { for (int x = 1; x <= 9; x++) { tail[n][x].resize(tot); for (int i = 0; i < tot; i++) { if (n == 0) { tail[n][x][i] += (final[i] == x); } else { for (int d = 1; d <= 9; d++) { if (nxt[i][d] != -1) { tail[n][x][i] += tail[n - 1][x][nxt[i][d]]; } } } } } } int t; std::cin >> t; while (t--) { solve(); } return 0; } |