// Marcin Knapik // #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b) for (int i = 0; i < b; ++i) #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define mp make_pair #define pb push_back #define f first #define s second mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng) using ll = long long; // unordered_map<ll, int> cache; int policz(ll val) { if (val < 10) { return val; } // auto it = cache.find(val); // if (it != cache.end()) // { // return it->second; // } long long ilo = 1; while (val > 0) { ilo *= val % 10; val /= 10; } return policz(ilo); // return cache[val] = policz(ilo); } string n; map<ll, ll> dp[20]; // TODO map or unordered_map, vector<pair>? vector<pair<ll, ll>> dp_vec[20]; vector<ll> res(10); void dodaj(ll ilo, int len) { for (const auto &[prod, quant] : dp_vec[len]) { res[policz(prod * ilo)] += quant; } } void compute_result(int pos) { // add values starting from this position int max_val = (pos == 0) ? (n[0] - '0') - 1 : 9; for (int i = 1; i <= max_val; ++i) { dodaj(i, sz(n) - pos - 1); } if (pos > 0) { // add to the so far equal prefix long long prod_so_far = 1; for (int i = 0; i < pos; ++i) { prod_so_far *= n[i] - '0'; } int curr_digit = n[pos] - '0'; for (int i = 0; i < curr_digit; ++i) { dodaj(prod_so_far * i, sz(n) - pos - 1); } } } void solve() { cin >> n; // n = to_string(losuj(1, 1e18)); FOR(i, 10) { res[i] = 0; } for (int i = 0; i < sz(n); ++i) { compute_result(i); } ++res[policz(stoll(n))]; for (int i = 0; i < 10; ++i) { cout << res[i] << (i == 9 ? '\n' : ' '); } } void prep() { ll ilo = 10; dp[0][1] = 1; dp_vec[0].pb({1, 1}); for (int len = 1; len <= 18; ++len) { for (int i = 0; i <= 9; ++i) { for (auto &[prod, quant] : dp[len - 1]) { dp[len][prod * i] += quant; } } dp_vec[len].reserve(sz(dp[len])); for (auto & [prod, quant] : dp[len]) { dp_vec[len].pb({prod, quant}); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; prep(); FOR(test, 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 136 137 138 139 140 141 142 143 144 | // Marcin Knapik // #pragma GCC optimize("O3") #include <bits/stdc++.h> using namespace std; #define FOR(i, b) for (int i = 0; i < b; ++i) #define sz(s) (int)s.size() #define all(s) s.begin(), s.end() #define mp make_pair #define pb push_back #define f first #define s second mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); #define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng) using ll = long long; // unordered_map<ll, int> cache; int policz(ll val) { if (val < 10) { return val; } // auto it = cache.find(val); // if (it != cache.end()) // { // return it->second; // } long long ilo = 1; while (val > 0) { ilo *= val % 10; val /= 10; } return policz(ilo); // return cache[val] = policz(ilo); } string n; map<ll, ll> dp[20]; // TODO map or unordered_map, vector<pair>? vector<pair<ll, ll>> dp_vec[20]; vector<ll> res(10); void dodaj(ll ilo, int len) { for (const auto &[prod, quant] : dp_vec[len]) { res[policz(prod * ilo)] += quant; } } void compute_result(int pos) { // add values starting from this position int max_val = (pos == 0) ? (n[0] - '0') - 1 : 9; for (int i = 1; i <= max_val; ++i) { dodaj(i, sz(n) - pos - 1); } if (pos > 0) { // add to the so far equal prefix long long prod_so_far = 1; for (int i = 0; i < pos; ++i) { prod_so_far *= n[i] - '0'; } int curr_digit = n[pos] - '0'; for (int i = 0; i < curr_digit; ++i) { dodaj(prod_so_far * i, sz(n) - pos - 1); } } } void solve() { cin >> n; // n = to_string(losuj(1, 1e18)); FOR(i, 10) { res[i] = 0; } for (int i = 0; i < sz(n); ++i) { compute_result(i); } ++res[policz(stoll(n))]; for (int i = 0; i < 10; ++i) { cout << res[i] << (i == 9 ? '\n' : ' '); } } void prep() { ll ilo = 10; dp[0][1] = 1; dp_vec[0].pb({1, 1}); for (int len = 1; len <= 18; ++len) { for (int i = 0; i <= 9; ++i) { for (auto &[prod, quant] : dp[len - 1]) { dp[len][prod * i] += quant; } } dp_vec[len].reserve(sz(dp[len])); for (auto & [prod, quant] : dp[len]) { dp_vec[len].pb({prod, quant}); } } } int main() { ios::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; prep(); FOR(test, t) { solve(); } return 0; } |