#include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef long double ld; template <class T> using VV = vector<vector<T>>; using VI = vector<int>; using VVI = vector<vector<int>>; using VL = vector<long long>; using VVL = vector<vector<long long>>; using VC = vector<char>; using VVC = vector<vector<char>>; using VB = vector<bool>; using VVB = vector<vector<bool>>; using VD = vector<double>; using VVD = vector<vector<double>>; using PII = pair<int, int>; using PLL = pair<long long, long long>; using PIL = pair<int, long long>; using PLI = pair<long long, int>; using VPII = vector<pair<int, int>>; using VPLL = vector<pair<long long, long long>>; #define LINE '\n' #define SPACE ' ' #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (int(b)); i++) #define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define sq(a) ((a) * (a)) #define sz(x) ((int)x.size()) #define fastio() \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define debug(x) cerr << #x << " = " << x << endl; const ll MOD = 1e9 + 7; template <class T> inline void maxi(T &a, T b) { a = max(a, b); } template <class T> inline void mini(T &a, T b) { a = min(a, b); } template <class T> inline void addi(T &a, T b) { a = (a + b) % MOD; } template <class T> inline void subi(T &a, T b) { a = (a - b + MOD) % MOD; } template <class T> inline T add(T a, T b) { return (a + b) % MOD; } template <class T> inline T sub(T a, T b) { return (a - b + MOD) % MOD; } template <class T> inline T mul(T a, T b) { return ((a % MOD) * (b % MOD)) % MOD; } const ll MAX_N = 1e18; const int MAX_LEN = 18; map<ll, ll> dpraw[MAX_LEN + 1]; VPLL dp[MAX_LEN + 1]; VPLL states; VI num; int len; VVL ctValids(MAX_LEN + 1, VL(10, 0)); int getDigit(ll num) { if(num == 0) return 0; while(num >= 10) { ll prod = 1; ll temp = num; while(temp > 0) { prod *= (temp % 10); temp /= 10; } num = prod; } return num; } VI getNum(ll num) { VI res; if (num == 0) res = {0}; while (num > 0) { res.PB(num % 10); num /= 10; } reverse(ALL(res)); return res; } // liczy dla dlugosci 1...Len ile mozna uzyskac roznych prod. // pozwala na zera wiodące void comp_dpraw() { dpraw[0][1] = 1; FOR(i, 0, 10) { dpraw[1][i] = 1; } FOR(i, 2, MAX_LEN+1) { // digit at the curr pos i FOR(d, 0, 10) { // for each digit multiply by all stany z dp[i-1] for (auto [key, val] : dpraw[i - 1]) { ll newkey = key * (ll)d; if(newkey % 10 == 0) newkey = 0; dpraw[i][newkey] += val; } } } } // mnozy sufiks przez prefiks prod void update_states(ll prod, const VPLL &st) { ll zeroct = 0; for (auto &[pr, ct] : st) { ll newprod = pr * prod; if(newprod % 10 == 0) zeroct += ct; else states.PB({newprod, ct}); } states.PB({0, zeroct}); } VL statesToCter() { VL res(10, 0); map<ll, ll> mp; for (auto [prod, ct] : states) { mp[prod] += ct; } int c = 0; for (auto [prod, ct] : mp) { assert(prod >= 0); assert(prod <= 1e18); int d = getDigit(prod); res[d] += ct; c += d == 0; } //debug(sz(states)); return res; } void calcForLen(int L) { if (L == 1) { FOR(i, 1, 10) ctValids[L][i] = 1; return; } // digit to make it valid states.clear(); FOR(d, 1, 10) { update_states(d, dp[L - 1]); } ctValids[L] = statesToCter(); } void calc_dp() { // iterowanie po rozpatrywanej cyfrze ll prod = 1; FOR(i, 0, len) { int sufit = num[i]; int beg = (i == 0 ? 1 : 0); FOR(d, beg, sufit) { ll newprod = prod * d; assert(newprod >= 0); int left = len - 1 - i; // zostalo na suffiksie if (left == 0) states.PB({newprod, 1}); else update_states(newprod, dp[left]); } prod *= sufit; } assert(prod >= 0); states.PB({prod, 1}); } void solve() { int t; cin >> t; comp_dpraw(); FOR(i, 0, MAX_LEN + 1) { for (auto &[key, value] : dpraw[i]) { dp[i].PB({key, value}); } } FOR(L, 1, MAX_LEN + 1) { calcForLen(L); } FOR(L, 1, MAX_LEN + 1) { FOR(d, 0, 10) ctValids[L][d] += ctValids[L-1][d]; } while (t--) { ll n; cin >> n; num = getNum(n); len = sz(num); states.clear(); calc_dp(); VL cter = statesToCter(); FOR(d, 0, 10) cter[d] += ctValids[len-1][d]; if(n == (ll)1e18) cter[0]++; FOR(i, 0, 10) { cout << cter[i] << SPACE; } cout << LINE; } } int main() { fastio(); 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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | #include <bits/stdc++.h> // #include <ext/pb_ds/assoc_container.hpp> // #include <ext/pb_ds/tree_policy.hpp> // #include <ext/rope> using namespace std; // using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; typedef long double ld; template <class T> using VV = vector<vector<T>>; using VI = vector<int>; using VVI = vector<vector<int>>; using VL = vector<long long>; using VVL = vector<vector<long long>>; using VC = vector<char>; using VVC = vector<vector<char>>; using VB = vector<bool>; using VVB = vector<vector<bool>>; using VD = vector<double>; using VVD = vector<vector<double>>; using PII = pair<int, int>; using PLL = pair<long long, long long>; using PIL = pair<int, long long>; using PLI = pair<long long, int>; using VPII = vector<pair<int, int>>; using VPLL = vector<pair<long long, long long>>; #define LINE '\n' #define SPACE ' ' #define PB push_back #define FOR(i, a, b) for (int i = (a); i < (int(b)); i++) #define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++) #define ALL(x) x.begin(), x.end() #define RALL(x) x.rbegin(), x.rend() #define sq(a) ((a) * (a)) #define sz(x) ((int)x.size()) #define fastio() \ ios_base::sync_with_stdio(false); \ cin.tie(nullptr); \ cout.tie(nullptr) #define debug(x) cerr << #x << " = " << x << endl; const ll MOD = 1e9 + 7; template <class T> inline void maxi(T &a, T b) { a = max(a, b); } template <class T> inline void mini(T &a, T b) { a = min(a, b); } template <class T> inline void addi(T &a, T b) { a = (a + b) % MOD; } template <class T> inline void subi(T &a, T b) { a = (a - b + MOD) % MOD; } template <class T> inline T add(T a, T b) { return (a + b) % MOD; } template <class T> inline T sub(T a, T b) { return (a - b + MOD) % MOD; } template <class T> inline T mul(T a, T b) { return ((a % MOD) * (b % MOD)) % MOD; } const ll MAX_N = 1e18; const int MAX_LEN = 18; map<ll, ll> dpraw[MAX_LEN + 1]; VPLL dp[MAX_LEN + 1]; VPLL states; VI num; int len; VVL ctValids(MAX_LEN + 1, VL(10, 0)); int getDigit(ll num) { if(num == 0) return 0; while(num >= 10) { ll prod = 1; ll temp = num; while(temp > 0) { prod *= (temp % 10); temp /= 10; } num = prod; } return num; } VI getNum(ll num) { VI res; if (num == 0) res = {0}; while (num > 0) { res.PB(num % 10); num /= 10; } reverse(ALL(res)); return res; } // liczy dla dlugosci 1...Len ile mozna uzyskac roznych prod. // pozwala na zera wiodące void comp_dpraw() { dpraw[0][1] = 1; FOR(i, 0, 10) { dpraw[1][i] = 1; } FOR(i, 2, MAX_LEN+1) { // digit at the curr pos i FOR(d, 0, 10) { // for each digit multiply by all stany z dp[i-1] for (auto [key, val] : dpraw[i - 1]) { ll newkey = key * (ll)d; if(newkey % 10 == 0) newkey = 0; dpraw[i][newkey] += val; } } } } // mnozy sufiks przez prefiks prod void update_states(ll prod, const VPLL &st) { ll zeroct = 0; for (auto &[pr, ct] : st) { ll newprod = pr * prod; if(newprod % 10 == 0) zeroct += ct; else states.PB({newprod, ct}); } states.PB({0, zeroct}); } VL statesToCter() { VL res(10, 0); map<ll, ll> mp; for (auto [prod, ct] : states) { mp[prod] += ct; } int c = 0; for (auto [prod, ct] : mp) { assert(prod >= 0); assert(prod <= 1e18); int d = getDigit(prod); res[d] += ct; c += d == 0; } //debug(sz(states)); return res; } void calcForLen(int L) { if (L == 1) { FOR(i, 1, 10) ctValids[L][i] = 1; return; } // digit to make it valid states.clear(); FOR(d, 1, 10) { update_states(d, dp[L - 1]); } ctValids[L] = statesToCter(); } void calc_dp() { // iterowanie po rozpatrywanej cyfrze ll prod = 1; FOR(i, 0, len) { int sufit = num[i]; int beg = (i == 0 ? 1 : 0); FOR(d, beg, sufit) { ll newprod = prod * d; assert(newprod >= 0); int left = len - 1 - i; // zostalo na suffiksie if (left == 0) states.PB({newprod, 1}); else update_states(newprod, dp[left]); } prod *= sufit; } assert(prod >= 0); states.PB({prod, 1}); } void solve() { int t; cin >> t; comp_dpraw(); FOR(i, 0, MAX_LEN + 1) { for (auto &[key, value] : dpraw[i]) { dp[i].PB({key, value}); } } FOR(L, 1, MAX_LEN + 1) { calcForLen(L); } FOR(L, 1, MAX_LEN + 1) { FOR(d, 0, 10) ctValids[L][d] += ctValids[L-1][d]; } while (t--) { ll n; cin >> n; num = getNum(n); len = sz(num); states.clear(); calc_dp(); VL cter = statesToCter(); FOR(d, 0, 10) cter[d] += ctValids[len-1][d]; if(n == (ll)1e18) cter[0]++; FOR(i, 0, 10) { cout << cter[i] << SPACE; } cout << LINE; } } int main() { fastio(); solve(); } |