#include <bits/stdc++.h> #define f first #define s second #define ull unsigned long long using namespace std; ull pack(ull a, ull b, ull c, ull d) { return ((a << 48) | (b << 32) | (c << 16) | d); } ull roz[10][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 1, 0, 0}, {2, 0, 0, 0}, {0, 0, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 1}, {3, 0, 0, 0}, {0, 2, 0, 0} }; ull updull(ull st, ull dg) { ull a = st >> 48; ull b = (st >> 32) & 65535; ull c = (st >> 16) & 65535; ull d = st & 65535; a += roz[dg][0]; b += roz[dg][1]; c += roz[dg][2]; d += roz[dg][3]; //cout << a << ' ' << b << ' ' << c << ' ' << d << '\n'; return pack(a, b, c, d); } ull pwr(ull base, ull exp) { if (exp == 0) return 1; if (exp == 1) return base; if (exp % 2 == 0) { ull half = pwr(base, exp / 2); return half * half; } return base * pwr(base, exp - 1); } ull dprod(ull x) { if (x == 0) return 0; ull prod = 1; while (x > 0) { ull dg = x % 10; prod *= dg; x /= 10; } return prod; } unordered_map<ull, ull> memo; ull mdr(ull num) { if (memo.count(num)) return memo[num]; ull a = num >> 48; ull b = (num >> 32) & 65535; ull c = (num >> 16) & 65535; ull d = num & 65535; if (a > 0 and c > 0) return memo[num] = 0; if (a == 0 and b == 0 and c == 0 and d == 0) return memo[num] = 1; if (a == 1 and b == 0 and c == 0 and d == 0) return memo[num] = 2; if (a == 0 and b == 1 and c == 0 and d == 0) return memo[num] = 3; if (a == 2 and b == 0 and c == 0 and d == 0) return memo[num] = 4; if (a == 0 and b == 0 and c == 1 and d == 0) return memo[num] = 5; if (a == 1 and b == 1 and c == 0 and d == 0) return memo[num] = 6; if (a == 0 and b == 0 and c == 0 and d == 1) return memo[num] = 7; if (a == 3 and b == 0 and c == 0 and d == 0) return memo[num] = 8; if (a == 0 and b == 2 and c == 0 and d == 0) return memo[num] = 9; ull prod = pwr(2, a) * pwr(3, b) * pwr(5, c) * pwr(7, d); //cout << prod << '\n'; while (prod >= 10) prod = dprod(prod); ull ans = (ull)prod; return memo[num] = ans; } int main() { cin.tie(0)->sync_with_stdio(0); memo.clear(); ull t; cin >> t; while (t--) { string num; cin >> num; ull L = num.size(); vector<unordered_map<ull, ull>> dpnz(4); vector<ull> dpz(4, 0); dpnz[2][ pack(0, 0, 0, 0) ] = 1; dpz[2] = 0; for (ull pos = 0; pos < L; pos++) { ull lim = num[pos] - '0'; vector<unordered_map<ull, ull>> nnz(4); vector<ull> nz(4, 0); for (ull i = 0; i < 4; i++) { ull ct = i >> 1; ull cs = i & 1; for (auto &j : dpnz[i]) { ull curp = j.f; ull ways = j.s; ull maxd = 9; if (ct) maxd = lim; for (ull dig = 0; dig <= maxd; dig++) { ull newt = 0; if (ct and (dig == lim)) newt = 1; ull news = cs; //cout << newt << ' ' << news << '\n'; if (!cs and dig > 0) news = 1; ull newull = (newt << 1) | news; if (!news) nnz[newull][ pack(0, 0, 0, 0) ] += ways; else { if (dig == 0) nz[newull] += ways; else { ull np = updull(curp, dig); nnz[newull][np] += ways; } } } } ull zeroval = dpz[i]; //cout << zeroval << '\n'; if (zeroval > 0) { ull maxd = 9; if (ct) maxd = lim; //cout << maxd << '\n'; for (ull dig = 0; dig <= maxd; dig++) { ull newt = 0; if (ct and (dig == lim)) newt = 1; ull news = cs; if (!cs and dig > 0) news = 1; ull newull = (newt << 1) | news; nz[newull] += zeroval; } } } dpnz = move(nnz); dpz = move(nz); //cout << dpnz << ' ' << dpz << '\n' } vector<ull> wyn(10, 0); for (ull j = 0; j < 4; j++) { if ((j & 1) == 0) continue; wyn[0] += dpz[j]; //cout << "chuj"; for (auto &i : dpnz[j]) { ull ld = mdr(i.f); wyn[ld] += i.s; } } for (ull d = 0; d < 10; d++) cout << wyn[d] << ' '; 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 | #include <bits/stdc++.h> #define f first #define s second #define ull unsigned long long using namespace std; ull pack(ull a, ull b, ull c, ull d) { return ((a << 48) | (b << 32) | (c << 16) | d); } ull roz[10][4] = { {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 1, 0, 0}, {2, 0, 0, 0}, {0, 0, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 1}, {3, 0, 0, 0}, {0, 2, 0, 0} }; ull updull(ull st, ull dg) { ull a = st >> 48; ull b = (st >> 32) & 65535; ull c = (st >> 16) & 65535; ull d = st & 65535; a += roz[dg][0]; b += roz[dg][1]; c += roz[dg][2]; d += roz[dg][3]; //cout << a << ' ' << b << ' ' << c << ' ' << d << '\n'; return pack(a, b, c, d); } ull pwr(ull base, ull exp) { if (exp == 0) return 1; if (exp == 1) return base; if (exp % 2 == 0) { ull half = pwr(base, exp / 2); return half * half; } return base * pwr(base, exp - 1); } ull dprod(ull x) { if (x == 0) return 0; ull prod = 1; while (x > 0) { ull dg = x % 10; prod *= dg; x /= 10; } return prod; } unordered_map<ull, ull> memo; ull mdr(ull num) { if (memo.count(num)) return memo[num]; ull a = num >> 48; ull b = (num >> 32) & 65535; ull c = (num >> 16) & 65535; ull d = num & 65535; if (a > 0 and c > 0) return memo[num] = 0; if (a == 0 and b == 0 and c == 0 and d == 0) return memo[num] = 1; if (a == 1 and b == 0 and c == 0 and d == 0) return memo[num] = 2; if (a == 0 and b == 1 and c == 0 and d == 0) return memo[num] = 3; if (a == 2 and b == 0 and c == 0 and d == 0) return memo[num] = 4; if (a == 0 and b == 0 and c == 1 and d == 0) return memo[num] = 5; if (a == 1 and b == 1 and c == 0 and d == 0) return memo[num] = 6; if (a == 0 and b == 0 and c == 0 and d == 1) return memo[num] = 7; if (a == 3 and b == 0 and c == 0 and d == 0) return memo[num] = 8; if (a == 0 and b == 2 and c == 0 and d == 0) return memo[num] = 9; ull prod = pwr(2, a) * pwr(3, b) * pwr(5, c) * pwr(7, d); //cout << prod << '\n'; while (prod >= 10) prod = dprod(prod); ull ans = (ull)prod; return memo[num] = ans; } int main() { cin.tie(0)->sync_with_stdio(0); memo.clear(); ull t; cin >> t; while (t--) { string num; cin >> num; ull L = num.size(); vector<unordered_map<ull, ull>> dpnz(4); vector<ull> dpz(4, 0); dpnz[2][ pack(0, 0, 0, 0) ] = 1; dpz[2] = 0; for (ull pos = 0; pos < L; pos++) { ull lim = num[pos] - '0'; vector<unordered_map<ull, ull>> nnz(4); vector<ull> nz(4, 0); for (ull i = 0; i < 4; i++) { ull ct = i >> 1; ull cs = i & 1; for (auto &j : dpnz[i]) { ull curp = j.f; ull ways = j.s; ull maxd = 9; if (ct) maxd = lim; for (ull dig = 0; dig <= maxd; dig++) { ull newt = 0; if (ct and (dig == lim)) newt = 1; ull news = cs; //cout << newt << ' ' << news << '\n'; if (!cs and dig > 0) news = 1; ull newull = (newt << 1) | news; if (!news) nnz[newull][ pack(0, 0, 0, 0) ] += ways; else { if (dig == 0) nz[newull] += ways; else { ull np = updull(curp, dig); nnz[newull][np] += ways; } } } } ull zeroval = dpz[i]; //cout << zeroval << '\n'; if (zeroval > 0) { ull maxd = 9; if (ct) maxd = lim; //cout << maxd << '\n'; for (ull dig = 0; dig <= maxd; dig++) { ull newt = 0; if (ct and (dig == lim)) newt = 1; ull news = cs; if (!cs and dig > 0) news = 1; ull newull = (newt << 1) | news; nz[newull] += zeroval; } } } dpnz = move(nnz); dpz = move(nz); //cout << dpnz << ' ' << dpz << '\n' } vector<ull> wyn(10, 0); for (ull j = 0; j < 4; j++) { if ((j & 1) == 0) continue; wyn[0] += dpz[j]; //cout << "chuj"; for (auto &i : dpnz[j]) { ull ld = mdr(i.f); wyn[ld] += i.s; } } for (ull d = 0; d < 10; d++) cout << wyn[d] << ' '; cout << '\n'; } } |