//#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; const int SZ_BIG = 36100; int nxt_[SZ_BIG][10]; ll val_[SZ_BIG]; int who_[SZ_BIG]; int calc(ll x) { if (x < 10) return x; ll prod = 1; while (x) { prod *= (x % 10); x /= 10; } return calc(prod); } bool good_[SZ_BIG]; const int SZ = 2088; int nxt[SZ][10]; ll val[SZ]; int who[SZ]; void prep() { set<ll> nums{1}; for (int i = 0; i < 18; i++) { set<ll> nnums; for (ll x : nums) { for (int p = 1; p <= 9; p++) nnums.insert(p * x); } swap(nums, nnums); } debug(nums.size()); assert(nums.size() == SZ_BIG); vector<ll> vec; for (ll x : nums) vec.emplace_back(x); for (int i = 0; i < vec.size(); i++) { val_[i] = vec[i]; for (int j = 1; j <= 9; j++) { ll new_val = vec[i] * j; int res = lower_bound(vec.begin(), vec.end(), new_val) - vec.begin(); if (res == vec.size() || vec[res] != new_val) { nxt_[i][j] = -1; } else { nxt_[i][j] = res; } } } for (int i = 0; i < vec.size(); i++) { ll x = vec[i]; who_[i] = calc(x); } for (int i = SZ_BIG - 1; i >= 0; i--) { good_[i] = (who_[i] > 0); for (int j = 1; j <= 9; j++) { if (nxt_[i][j] != -1 && good_[nxt_[i][j]]) good_[i] = true; } } int f = 0; vec.clear(); for (int i = 0; i < SZ_BIG; i++) { if (good_[i]) { vec.emplace_back(val_[i]); f++; } } assert(f == 2088); for (int i = 0; i < vec.size(); i++) { val[i] = vec[i]; for (int j = 1; j <= 9; j++) { ll new_val = vec[i] * j; int res = lower_bound(vec.begin(), vec.end(), new_val) - vec.begin(); if (res == vec.size() || vec[res] != new_val) { nxt[i][j] = -1; } else { nxt[i][j] = res; } } } for (int i = 0; i < vec.size(); i++) { ll x = vec[i]; who[i] = calc(x); } } ll n; int dig[22]; ll dp[2][SZ][2]; ll tot[10]; void solve() { cin >> n; string s = to_string(n); if (s.size() == 19) { s = to_string(n - 1); } assert((int)s.size() <= 18); for (int i = 0; i < s.size(); i++) { dig[i] = s[i] - '0'; } memset(dp, 0, sizeof dp); memset(tot, 0, sizeof tot); dp[0][0][1] = 1; ll lim = 1; int tp = 0; for (int i = s.size() - 1; i >= 0; i--) { int x = dig[i]; for (int c = 0; c < SZ && val[c] <= 9 * lim; c++) { dp[tp ^ 1][c][0] = 0; dp[tp ^ 1][c][1] = 0; } for (int c = 0; c < SZ && val[c] <= lim; c++) { for (int fl = 0; fl < 2; fl++) { if (!dp[tp][c][fl]) continue; for (int we = 1; we <= 9; we++) { int where = nxt[c][we]; if (where == -1) continue; int nfl = (we < x || (fl && (we == x))); dp[tp ^ 1][where][nfl] += dp[tp][c][fl]; } } } lim *= 9; tp ^= 1; for (int c = 0; c < SZ && val[c] <= lim; c++) { for (int fl = 0; fl < 2; fl++) { if (!fl && (i == 0)) continue; tot[who[c]] += dp[tp][c][fl]; } } } tot[0] = n; for (int i = 1; i <= 9; i++) tot[0] -= tot[i]; for (int i = 0; i <= 9; i++) cout << tot[i] << " "; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); auto start = std::chrono::high_resolution_clock::now(); #ifdef DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif prep(); int tst; cin >> tst; while (tst--) solve(); auto end = std::chrono::high_resolution_clock::now(); std::cerr << "Execution Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; 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 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 | //#ifdef DEBUG //#define _GLIBCXX_DEBUG //#endif //#pragma GCC optimize("O3") #include<bits/stdc++.h> using namespace std; #ifdef DEBUG #include "lib/debug.h" #else #define debug(...) 228 #endif #define pb push_back typedef long long ll; typedef long double ld; const int SZ_BIG = 36100; int nxt_[SZ_BIG][10]; ll val_[SZ_BIG]; int who_[SZ_BIG]; int calc(ll x) { if (x < 10) return x; ll prod = 1; while (x) { prod *= (x % 10); x /= 10; } return calc(prod); } bool good_[SZ_BIG]; const int SZ = 2088; int nxt[SZ][10]; ll val[SZ]; int who[SZ]; void prep() { set<ll> nums{1}; for (int i = 0; i < 18; i++) { set<ll> nnums; for (ll x : nums) { for (int p = 1; p <= 9; p++) nnums.insert(p * x); } swap(nums, nnums); } debug(nums.size()); assert(nums.size() == SZ_BIG); vector<ll> vec; for (ll x : nums) vec.emplace_back(x); for (int i = 0; i < vec.size(); i++) { val_[i] = vec[i]; for (int j = 1; j <= 9; j++) { ll new_val = vec[i] * j; int res = lower_bound(vec.begin(), vec.end(), new_val) - vec.begin(); if (res == vec.size() || vec[res] != new_val) { nxt_[i][j] = -1; } else { nxt_[i][j] = res; } } } for (int i = 0; i < vec.size(); i++) { ll x = vec[i]; who_[i] = calc(x); } for (int i = SZ_BIG - 1; i >= 0; i--) { good_[i] = (who_[i] > 0); for (int j = 1; j <= 9; j++) { if (nxt_[i][j] != -1 && good_[nxt_[i][j]]) good_[i] = true; } } int f = 0; vec.clear(); for (int i = 0; i < SZ_BIG; i++) { if (good_[i]) { vec.emplace_back(val_[i]); f++; } } assert(f == 2088); for (int i = 0; i < vec.size(); i++) { val[i] = vec[i]; for (int j = 1; j <= 9; j++) { ll new_val = vec[i] * j; int res = lower_bound(vec.begin(), vec.end(), new_val) - vec.begin(); if (res == vec.size() || vec[res] != new_val) { nxt[i][j] = -1; } else { nxt[i][j] = res; } } } for (int i = 0; i < vec.size(); i++) { ll x = vec[i]; who[i] = calc(x); } } ll n; int dig[22]; ll dp[2][SZ][2]; ll tot[10]; void solve() { cin >> n; string s = to_string(n); if (s.size() == 19) { s = to_string(n - 1); } assert((int)s.size() <= 18); for (int i = 0; i < s.size(); i++) { dig[i] = s[i] - '0'; } memset(dp, 0, sizeof dp); memset(tot, 0, sizeof tot); dp[0][0][1] = 1; ll lim = 1; int tp = 0; for (int i = s.size() - 1; i >= 0; i--) { int x = dig[i]; for (int c = 0; c < SZ && val[c] <= 9 * lim; c++) { dp[tp ^ 1][c][0] = 0; dp[tp ^ 1][c][1] = 0; } for (int c = 0; c < SZ && val[c] <= lim; c++) { for (int fl = 0; fl < 2; fl++) { if (!dp[tp][c][fl]) continue; for (int we = 1; we <= 9; we++) { int where = nxt[c][we]; if (where == -1) continue; int nfl = (we < x || (fl && (we == x))); dp[tp ^ 1][where][nfl] += dp[tp][c][fl]; } } } lim *= 9; tp ^= 1; for (int c = 0; c < SZ && val[c] <= lim; c++) { for (int fl = 0; fl < 2; fl++) { if (!fl && (i == 0)) continue; tot[who[c]] += dp[tp][c][fl]; } } } tot[0] = n; for (int i = 1; i <= 9; i++) tot[0] -= tot[i]; for (int i = 0; i <= 9; i++) cout << tot[i] << " "; cout << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); auto start = std::chrono::high_resolution_clock::now(); #ifdef DEBUG freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif prep(); int tst; cin >> tst; while (tst--) solve(); auto end = std::chrono::high_resolution_clock::now(); std::cerr << "Execution Time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl; return 0; } |