#include <iostream> #include <map> using namespace std; map<int, long long> count(long long n, int exp, bool count0, bool cach); int getDigit(int factor); int getFactor(int f2, int f3, int f5, int f7); int getDigitFromMul(long long mul); int getExp(long long n); long long ex(int e); int add(int factor, int f2, int f3, int f5, int f7); long long getZeros(map<int, long long int> &factors); class Key { public: long long n; int exp; bool count0; bool operator<(const Key &other) const { if(count0 == other.count0) { if(exp == other.exp) { return n < other.n; } return exp < other.exp; } return count0; } }; map<Key, map<int, long long>> cache; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; long long n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; int exp = getExp(n); map<int, long long> factors = count(n, exp, false, false); factors[getFactor(1, 0, 1, 0)]--; long long digits[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (auto const &factor: factors) { int d = getDigit(factor.first); digits[d] += factor.second; } for (int i = 0; i < 10; i++) { if (i > 0) { cout << " "; } cout << digits[i]; } cout << '\n'; } return 0; } long long getZeros(map<int, long long int> &factors) { long long digits[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (auto const &factor: factors) { int d = getDigit(factor.first); digits[d] += factor.second; } return digits[0]; } map<int, long long> count(long long n, int exp, bool count0, bool cach) { auto key = Key(n, exp, count0); auto it = cache.find(key); if(it != cache.end()) { return (*it).second; } map<int, long long> m; if (n <= 9 && exp==0) { if (n >= 0) m[getFactor(1, 0, 1, 0)]++; if (n >= 1) m[getFactor(0, 0, 0, 0)]++; if (n >= 2) m[getFactor(1, 0, 0, 0)]++; if (n >= 3) m[getFactor(0, 1, 0, 0)]++; if (n >= 4) m[getFactor(2, 0, 0, 0)]++; if (n >= 5) m[getFactor(0, 0, 1, 0)]++; if (n >= 6) m[getFactor(1, 1, 0, 0)]++; if (n >= 7) m[getFactor(0, 0, 0, 1)]++; if (n >= 8) m[getFactor(3, 0, 0, 0)]++; if (n >= 9) m[getFactor(0, 2, 0, 0)]++; } else { long long pattern = ex(exp); long long de = n / pattern; int digit = (int) de; long long lower = pattern - 1; map<int, long long> m2 = count(lower, exp - 1, false, true); map<int, long long> m3 = count(lower, exp - 1, true, true); map<int, long long> rest = count(n - (digit * pattern), exp - 1, true, false); if (digit > 0) { if (count0) { for (auto const &factor: m2) { m[add(factor.first, 1, 0, 1, 0)] += factor.second; } } else { for (auto const &factor: m2) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } } else if (digit == 0) { if (count0) { for (auto const &factor: rest) { m[add(factor.first, 1, 0, 1, 0)] += factor.second; } } else { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } } if (digit > 1) { for (auto const &factor: m3) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } else if (digit == 1) { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } if (digit > 2) { for (auto const &factor: m3) { m[add(factor.first, 1, 0, 0, 0)] += factor.second; } } else if (digit == 2) { for (auto const &factor: rest) { m[add(factor.first, 1, 0, 0, 0)] += factor.second; } } if (digit > 3) { for (auto const &factor: m3) { m[add(factor.first, 0, 1, 0, 0)] += factor.second; } } else if (digit == 3) { for (auto const &factor: rest) { m[add(factor.first, 0, 1, 0, 0)] += factor.second; } } if (digit > 4) { for (auto const &factor: m3) { m[add(factor.first, 2, 0, 0, 0)] += factor.second; } } else if (digit == 4) { for (auto const &factor: rest) { m[add(factor.first, 2, 0, 0, 0)] += factor.second; } } if (digit > 5) { for (auto const &factor: m3) { m[add(factor.first, 0, 0, 1, 0)] += factor.second; } } else if (digit == 5) { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 1, 0)] += factor.second; } } if (digit > 6) { for (auto const &factor: m3) { m[add(factor.first, 1, 1, 0, 0)] += factor.second; } } else if (digit == 6) { for (auto const &factor: rest) { m[add(factor.first, 1, 1, 0, 0)] += factor.second; } } if (digit > 7) { for (auto const &factor: m3) { m[add(factor.first, 0, 0, 0, 1)] += factor.second; } } else if (digit == 7) { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 0, 1)] += factor.second; } } if (digit > 8) { for (auto const &factor: m3) { m[add(factor.first, 3, 0, 0, 0)] += factor.second; } } else if (digit == 8) { for (auto const &factor: rest) { m[add(factor.first, 3, 0, 0, 0)] += factor.second; } } if (digit == 9) { for (auto const &factor: rest) { m[add(factor.first, 0, 2, 0, 0)] += factor.second; } } } if(cach) { cache[key] = m; } return m; } int getDigit(int factor) { int f2 = factor & 0xff; int f3 = (factor >> 8) & 0xff; int f5 = (factor >> 16) & 0xff; int f7 = (factor >> 24) & 0xff; long long mul = 1; for (int i = 0; i < f2; i++) { mul *= 2; } for (int i = 0; i < f3; i++) { mul *= 3; } for (int i = 0; i < f5; i++) { mul *= 5; } for (int i = 0; i < f7; i++) { mul *= 7; } return getDigitFromMul(mul); } int getDigitFromMul(long long mul) { while (mul > 9) { long long n = 1; while (mul > 0) { n *= mul % 10; mul /= 10; } mul = n; } return mul; } int getFactor(int f2, int f3, int f5, int f7) { return f2 | f3 << 8 | f5 << 16 | f7 << 24; } int add(int factor, int f2, int f3, int f5, int f7) { return factor + f2 + (f3 << 8) + (f5 << 16) + (f7 << 24); } int getExp(long long n) { int exp = -1; while (n > 0) { n /= 10; exp++; } return exp; } long long ex(int e) { long long res = 1; for (int i = 0; i < e; i++) { res *= 10; } return res; }
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 | #include <iostream> #include <map> using namespace std; map<int, long long> count(long long n, int exp, bool count0, bool cach); int getDigit(int factor); int getFactor(int f2, int f3, int f5, int f7); int getDigitFromMul(long long mul); int getExp(long long n); long long ex(int e); int add(int factor, int f2, int f3, int f5, int f7); long long getZeros(map<int, long long int> &factors); class Key { public: long long n; int exp; bool count0; bool operator<(const Key &other) const { if(count0 == other.count0) { if(exp == other.exp) { return n < other.n; } return exp < other.exp; } return count0; } }; map<Key, map<int, long long>> cache; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; long long n; cin >> t; for (int i = 0; i < t; i++) { cin >> n; int exp = getExp(n); map<int, long long> factors = count(n, exp, false, false); factors[getFactor(1, 0, 1, 0)]--; long long digits[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (auto const &factor: factors) { int d = getDigit(factor.first); digits[d] += factor.second; } for (int i = 0; i < 10; i++) { if (i > 0) { cout << " "; } cout << digits[i]; } cout << '\n'; } return 0; } long long getZeros(map<int, long long int> &factors) { long long digits[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; for (auto const &factor: factors) { int d = getDigit(factor.first); digits[d] += factor.second; } return digits[0]; } map<int, long long> count(long long n, int exp, bool count0, bool cach) { auto key = Key(n, exp, count0); auto it = cache.find(key); if(it != cache.end()) { return (*it).second; } map<int, long long> m; if (n <= 9 && exp==0) { if (n >= 0) m[getFactor(1, 0, 1, 0)]++; if (n >= 1) m[getFactor(0, 0, 0, 0)]++; if (n >= 2) m[getFactor(1, 0, 0, 0)]++; if (n >= 3) m[getFactor(0, 1, 0, 0)]++; if (n >= 4) m[getFactor(2, 0, 0, 0)]++; if (n >= 5) m[getFactor(0, 0, 1, 0)]++; if (n >= 6) m[getFactor(1, 1, 0, 0)]++; if (n >= 7) m[getFactor(0, 0, 0, 1)]++; if (n >= 8) m[getFactor(3, 0, 0, 0)]++; if (n >= 9) m[getFactor(0, 2, 0, 0)]++; } else { long long pattern = ex(exp); long long de = n / pattern; int digit = (int) de; long long lower = pattern - 1; map<int, long long> m2 = count(lower, exp - 1, false, true); map<int, long long> m3 = count(lower, exp - 1, true, true); map<int, long long> rest = count(n - (digit * pattern), exp - 1, true, false); if (digit > 0) { if (count0) { for (auto const &factor: m2) { m[add(factor.first, 1, 0, 1, 0)] += factor.second; } } else { for (auto const &factor: m2) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } } else if (digit == 0) { if (count0) { for (auto const &factor: rest) { m[add(factor.first, 1, 0, 1, 0)] += factor.second; } } else { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } } if (digit > 1) { for (auto const &factor: m3) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } else if (digit == 1) { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 0, 0)] += factor.second; } } if (digit > 2) { for (auto const &factor: m3) { m[add(factor.first, 1, 0, 0, 0)] += factor.second; } } else if (digit == 2) { for (auto const &factor: rest) { m[add(factor.first, 1, 0, 0, 0)] += factor.second; } } if (digit > 3) { for (auto const &factor: m3) { m[add(factor.first, 0, 1, 0, 0)] += factor.second; } } else if (digit == 3) { for (auto const &factor: rest) { m[add(factor.first, 0, 1, 0, 0)] += factor.second; } } if (digit > 4) { for (auto const &factor: m3) { m[add(factor.first, 2, 0, 0, 0)] += factor.second; } } else if (digit == 4) { for (auto const &factor: rest) { m[add(factor.first, 2, 0, 0, 0)] += factor.second; } } if (digit > 5) { for (auto const &factor: m3) { m[add(factor.first, 0, 0, 1, 0)] += factor.second; } } else if (digit == 5) { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 1, 0)] += factor.second; } } if (digit > 6) { for (auto const &factor: m3) { m[add(factor.first, 1, 1, 0, 0)] += factor.second; } } else if (digit == 6) { for (auto const &factor: rest) { m[add(factor.first, 1, 1, 0, 0)] += factor.second; } } if (digit > 7) { for (auto const &factor: m3) { m[add(factor.first, 0, 0, 0, 1)] += factor.second; } } else if (digit == 7) { for (auto const &factor: rest) { m[add(factor.first, 0, 0, 0, 1)] += factor.second; } } if (digit > 8) { for (auto const &factor: m3) { m[add(factor.first, 3, 0, 0, 0)] += factor.second; } } else if (digit == 8) { for (auto const &factor: rest) { m[add(factor.first, 3, 0, 0, 0)] += factor.second; } } if (digit == 9) { for (auto const &factor: rest) { m[add(factor.first, 0, 2, 0, 0)] += factor.second; } } } if(cach) { cache[key] = m; } return m; } int getDigit(int factor) { int f2 = factor & 0xff; int f3 = (factor >> 8) & 0xff; int f5 = (factor >> 16) & 0xff; int f7 = (factor >> 24) & 0xff; long long mul = 1; for (int i = 0; i < f2; i++) { mul *= 2; } for (int i = 0; i < f3; i++) { mul *= 3; } for (int i = 0; i < f5; i++) { mul *= 5; } for (int i = 0; i < f7; i++) { mul *= 7; } return getDigitFromMul(mul); } int getDigitFromMul(long long mul) { while (mul > 9) { long long n = 1; while (mul > 0) { n *= mul % 10; mul /= 10; } mul = n; } return mul; } int getFactor(int f2, int f3, int f5, int f7) { return f2 | f3 << 8 | f5 << 16 | f7 << 24; } int add(int factor, int f2, int f3, int f5, int f7) { return factor + f2 + (f3 << 8) + (f5 << 16) + (f7 << 24); } int getExp(long long n) { int exp = -1; while (n > 0) { n /= 10; exp++; } return exp; } long long ex(int e) { long long res = 1; for (int i = 0; i < e; i++) { res *= 10; } return res; } |