#include <iostream> #include <unordered_map> #include <vector> #include <algorithm> using namespace std; static const int64_t TEN_TO_18 = 1'000'000'000'000'000'000; static unordered_map<int64_t, int64_t> mapRes; static unordered_map<int64_t, int8_t> mapFinalDigits; static vector<int> vDigits; static int64_t aResult[10]; static int64_t aPowsOf10[20]; static bool fMaxNumber; static int iMaxDigitCnt; struct NumbersWithNDigits { unordered_map<int64_t, int64_t> map; // key: product of digits; value: count of numbers with this product of digits unordered_map<int64_t, int64_t> mapAggr; // key: product of digits; value: count of numbers with this product of digits void InitForOneDigit() { map.clear(); mapAggr.clear(); for (int i = 1; i <= 9; ++i) { map[i] = 1; mapAggr[i] = 1; } } void InitForMoreDigits(const NumbersWithNDigits & shorter) { map.clear(); mapAggr = shorter.mapAggr; for (const auto & [product, cnt] : shorter.map) { for (int i = 0; i <= 9; ++i) { int64_t x = product * i; map[x] += cnt; mapAggr[x] += cnt; } } } }; static NumbersWithNDigits aHists[18]; static void Init() { aHists[1].InitForOneDigit(); for (int i = 2; i <= iMaxDigitCnt - 1; ++i) { aHists[i].InitForMoreDigits(aHists[i - 1]); } aPowsOf10[0] = 1; for (int i = 1; i < 20; ++i) { aPowsOf10[i] = aPowsOf10[i - 1] * 10; } } static void PrepareVectorOfDigits(int64_t n) { vDigits.clear(); while (n != 0) { int iDigit = n % 10; vDigits.push_back(iDigit); n /= 10; } } static void InsertInResult(int64_t iMultiplierForProduct, int iDigitIndex) { const unordered_map<int64_t, int64_t> & mapBase = aHists[iDigitIndex].map; for (const auto & [product, cnt] : mapBase) { mapRes[product * iMultiplierForProduct] += cnt; } } static void InsertInResultAggr(int64_t iMultiplierForProduct, int iDigitIndex) { const unordered_map<int64_t, int64_t> & mapBase = aHists[iDigitIndex].mapAggr; for (const auto& [product, cnt] : mapBase) { mapRes[product * iMultiplierForProduct] += cnt; } } static void Recursive(int64_t iMultiplier, int iDigitIndex, bool fFirstDigit) { int iDigit = vDigits[iDigitIndex]; if (iDigitIndex == 0) // it is the last digit { for (int i = 1; i <= iDigit; ++i) { mapRes[iMultiplier * i]++; } return; } if (fFirstDigit) { InsertInResultAggr(iMultiplier, iDigitIndex); } if (!fFirstDigit && iDigit != 0) { mapRes[0] += (aPowsOf10[iDigitIndex] - 1); } for (int i = 1; i < iDigit; ++i) { if (iDigitIndex > 1) { mapRes[0] += (aPowsOf10[iDigitIndex - 1] - 1); } InsertInResult(iMultiplier * i, iDigitIndex); } mapRes[0] += iDigit; Recursive(iMultiplier * iDigit, iDigitIndex - 1, false); } static int GetDigitCount(int64_t iNum) { int iRet = 1; while (iNum > 9) { iNum /= 10; iRet++; } return iRet; } static int FindFinalDigit(int64_t iNum) { if (mapFinalDigits.count(iNum) != 0) { return mapFinalDigits[iNum]; } if (iNum < 10) { mapFinalDigits[iNum] = (int8_t) iNum; return (int) iNum; } int64_t iProduct = 1; for (int64_t i = iNum; i != 0; i /= 10) { int iDigit = i % 10; iProduct *= iDigit; } if (iProduct < 10) { mapFinalDigits[iNum] = (int8_t) iProduct; return (int) iProduct; } int iFinalDigit = FindFinalDigit(iProduct); mapFinalDigits[iNum] = iFinalDigit; return iFinalDigit; } static void OutputResult() { for (int i = 0; i <= 9; ++i) { aResult[i] = 0; } for (const auto & [product, cnt] : mapRes) { int iFinalDigit = FindFinalDigit(product); aResult[iFinalDigit] += cnt; } if (fMaxNumber) { aResult[0]++; } for (int i = 0; ; ++i) { cout << aResult[i]; if (i == 9) break; cout << " "; } } static void SolveCase(int64_t n) { mapRes.clear(); PrepareVectorOfDigits(n); Recursive(1, (int) vDigits.size() - 1, true); OutputResult(); } int main() { vector<int64_t> vInputs; int64_t t, n; cin >> t; iMaxDigitCnt = 1; for (int i = 1; i <= t; ++i) { cin >> n; vInputs.push_back(n); if (n == TEN_TO_18) // this is a special case { n--; } int iDigitCnt = GetDigitCount(n); if (iDigitCnt > iMaxDigitCnt) { iMaxDigitCnt = iDigitCnt; } } Init(); for (int i = 0; ; ++i) { n = vInputs[i]; fMaxNumber = false; if (n == TEN_TO_18) { n--; fMaxNumber = true; } SolveCase(n); if (i == vInputs.size() - 1) break; cout << endl; } }
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 | #include <iostream> #include <unordered_map> #include <vector> #include <algorithm> using namespace std; static const int64_t TEN_TO_18 = 1'000'000'000'000'000'000; static unordered_map<int64_t, int64_t> mapRes; static unordered_map<int64_t, int8_t> mapFinalDigits; static vector<int> vDigits; static int64_t aResult[10]; static int64_t aPowsOf10[20]; static bool fMaxNumber; static int iMaxDigitCnt; struct NumbersWithNDigits { unordered_map<int64_t, int64_t> map; // key: product of digits; value: count of numbers with this product of digits unordered_map<int64_t, int64_t> mapAggr; // key: product of digits; value: count of numbers with this product of digits void InitForOneDigit() { map.clear(); mapAggr.clear(); for (int i = 1; i <= 9; ++i) { map[i] = 1; mapAggr[i] = 1; } } void InitForMoreDigits(const NumbersWithNDigits & shorter) { map.clear(); mapAggr = shorter.mapAggr; for (const auto & [product, cnt] : shorter.map) { for (int i = 0; i <= 9; ++i) { int64_t x = product * i; map[x] += cnt; mapAggr[x] += cnt; } } } }; static NumbersWithNDigits aHists[18]; static void Init() { aHists[1].InitForOneDigit(); for (int i = 2; i <= iMaxDigitCnt - 1; ++i) { aHists[i].InitForMoreDigits(aHists[i - 1]); } aPowsOf10[0] = 1; for (int i = 1; i < 20; ++i) { aPowsOf10[i] = aPowsOf10[i - 1] * 10; } } static void PrepareVectorOfDigits(int64_t n) { vDigits.clear(); while (n != 0) { int iDigit = n % 10; vDigits.push_back(iDigit); n /= 10; } } static void InsertInResult(int64_t iMultiplierForProduct, int iDigitIndex) { const unordered_map<int64_t, int64_t> & mapBase = aHists[iDigitIndex].map; for (const auto & [product, cnt] : mapBase) { mapRes[product * iMultiplierForProduct] += cnt; } } static void InsertInResultAggr(int64_t iMultiplierForProduct, int iDigitIndex) { const unordered_map<int64_t, int64_t> & mapBase = aHists[iDigitIndex].mapAggr; for (const auto& [product, cnt] : mapBase) { mapRes[product * iMultiplierForProduct] += cnt; } } static void Recursive(int64_t iMultiplier, int iDigitIndex, bool fFirstDigit) { int iDigit = vDigits[iDigitIndex]; if (iDigitIndex == 0) // it is the last digit { for (int i = 1; i <= iDigit; ++i) { mapRes[iMultiplier * i]++; } return; } if (fFirstDigit) { InsertInResultAggr(iMultiplier, iDigitIndex); } if (!fFirstDigit && iDigit != 0) { mapRes[0] += (aPowsOf10[iDigitIndex] - 1); } for (int i = 1; i < iDigit; ++i) { if (iDigitIndex > 1) { mapRes[0] += (aPowsOf10[iDigitIndex - 1] - 1); } InsertInResult(iMultiplier * i, iDigitIndex); } mapRes[0] += iDigit; Recursive(iMultiplier * iDigit, iDigitIndex - 1, false); } static int GetDigitCount(int64_t iNum) { int iRet = 1; while (iNum > 9) { iNum /= 10; iRet++; } return iRet; } static int FindFinalDigit(int64_t iNum) { if (mapFinalDigits.count(iNum) != 0) { return mapFinalDigits[iNum]; } if (iNum < 10) { mapFinalDigits[iNum] = (int8_t) iNum; return (int) iNum; } int64_t iProduct = 1; for (int64_t i = iNum; i != 0; i /= 10) { int iDigit = i % 10; iProduct *= iDigit; } if (iProduct < 10) { mapFinalDigits[iNum] = (int8_t) iProduct; return (int) iProduct; } int iFinalDigit = FindFinalDigit(iProduct); mapFinalDigits[iNum] = iFinalDigit; return iFinalDigit; } static void OutputResult() { for (int i = 0; i <= 9; ++i) { aResult[i] = 0; } for (const auto & [product, cnt] : mapRes) { int iFinalDigit = FindFinalDigit(product); aResult[iFinalDigit] += cnt; } if (fMaxNumber) { aResult[0]++; } for (int i = 0; ; ++i) { cout << aResult[i]; if (i == 9) break; cout << " "; } } static void SolveCase(int64_t n) { mapRes.clear(); PrepareVectorOfDigits(n); Recursive(1, (int) vDigits.size() - 1, true); OutputResult(); } int main() { vector<int64_t> vInputs; int64_t t, n; cin >> t; iMaxDigitCnt = 1; for (int i = 1; i <= t; ++i) { cin >> n; vInputs.push_back(n); if (n == TEN_TO_18) // this is a special case { n--; } int iDigitCnt = GetDigitCount(n); if (iDigitCnt > iMaxDigitCnt) { iMaxDigitCnt = iDigitCnt; } } Init(); for (int i = 0; ; ++i) { n = vInputs[i]; fMaxNumber = false; if (n == TEN_TO_18) { n--; fMaxNumber = true; } SolveCase(n); if (i == vInputs.size() - 1) break; cout << endl; } } |