#include<iostream> #include<vector> #include<algorithm> #include <stdio.h> using namespace std; long long TWO = 2LL; long long THREE = 3LL; long long FIVE = 5LL; long long SEVEN = 7LL; long long TEN = 10LL; long long MAXN = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; long long MAXN_RESULT[10] = {999879067589400316LL, 18LL, 2795912956450LL, 171LL, 28611339267816LL, 78039555LL, 55909713312571LL, 171LL, 33615367021792LL, 1140LL}; long long P2[55]; long long P3[37]; long long P5[19]; long long P7[19]; long long P10[19]; long long RESULT_CACHE_P10[19][10]; long long getResult(long long n) { if (n >= 0 && n <= 9) { return n; } long long result = 1; while (n > 0) { long long digit = n % 10; if (digit == 0) { return 0; } else { n = n / 10LL; result = result * digit; } } return getResult(result); } long long NCHOOSE[19][19]; vector<int> digits(10); int numDigits(long long n) { for (int i = 17; i >= 0; i--) { if (P10[i] <= n) { return i + 1; } } return 0; } struct ND { int result; int count_2; int count_3; int count_5; int count_7; long long value; }; struct FD { // digit count int dc[10]; long long value; int result; int minDigits; void print() { for (int i = 2; i < 10; i++) { cout << i << ": " << dc[i] << ", "; } cout << "res: " << result << ", " << "val: " << value << endl; } int minNumDigits() { return minDigits; } long long getCount(vector<int>& digits) { long long product = 1LL; int topPart = 0; for (int i = 1; i < 10; i++) { topPart += digits[i]; } for (int i = 1; i < 10; i++) { product *= NCHOOSE[topPart][digits[i]]; topPart -= digits[i]; } return product; } long long countLessThanN(long long n, int numDigitsN, bool withCache) { if (value == 1LL) { int extra = withCache ? 1 : numDigitsN; for (int power = numDigitsN - 1; power >= 0; power--) { long long currentDigit = n / P10[power]; if (currentDigit > 1LL) { return extra; } else if (currentDigit < 1LL) { return extra - 1; } n -= currentDigit * P10[power]; } return extra; } if (n > P10[18]) { return 0LL; } if (minDigits > numDigitsN) { return 0LL; } long long count = 0LL; //cout<< "Value: " << value << endl; if (withCache && minDigits < numDigitsN) { long long product = 1LL; long long topPart = minDigits; for (int i = 2; i < 10; i++) { product *= NCHOOSE[topPart][dc[i]]; topPart -= dc[i]; } //cout<< "Product: " << product << endl; long long choice1 = 0LL; for (int i = minDigits; i < numDigitsN; i++) { choice1 += NCHOOSE[i][minDigits]; //cout<< "i: " << i << ", choice1 = " << choice1 << endl; } //cout<< "Choice1: " << choice1 << endl; count = product * choice1; } //cout<< "Count: " << count << endl; if (value == 1LL) { dc[1] = numDigitsN; } else { dc[1] = numDigitsN - minDigits; } for (int i = 1; i < 10; i++) { digits[i] = dc[i]; //cout<< i << ": " << digits[i] << ", "; } //cout<< "\n"; for (int i = numDigitsN - 1; i >= 0; i--) { long long power = P10[i]; //cout<< "Power: " << power << endl; long long currentDigit = n / power; //cout<< "Current digit: " << currentDigit << endl; if (currentDigit == 0) { return count; } for (int j = 1; j < currentDigit || (i == 0 && j == currentDigit); j++) { //cout<< "j: " << j << endl; if (digits[j] == 0) { continue; } else { digits[j] -= 1; long long newCount = getCount(digits); //cout<< "Extra count: " << newCount << endl; count += newCount; digits[j] += 1; } } if (digits[currentDigit] == 0) { return count; } digits[currentDigit] -= 1; n -= currentDigit * power; } return count; } }; bool comp(const ND& nd1, const ND& nd2) { return nd1.value < nd2.value; } bool is237Valid(int x2, int x3, int x7) { return x2 / 3 + x3 / 2 + x7 <= 18; } bool is537Valid(int x5, int x3, int x7) { return x5 + x3 / 2 + x7 <= 18; } vector<ND> NON_ZERO; vector<FD> FD_NZ; void fillPowers() { P2[0] = 1LL; P3[0] = 1LL; P5[0] = 1LL; P7[0] = 1LL; P10[0] = 1LL; for (int i = 1; i < 55; i++) { P2[i] = P2[i-1] * TWO; } for (int i = 1; i < 37; i++) { P3[i] = P3[i-1] * THREE; } for (int i = 1; i < 19; i++) { P5[i] = P5[i-1] * FIVE; P7[i] = P7[i-1] * SEVEN; P10[i] = P10[i-1] * TEN; } } void fillNChoose() { for (int i = 0; i <= 18; i++) { NCHOOSE[i][0] = 1LL; } for (int j = 1; j <= 18; j++) { NCHOOSE[0][j] = 0; } for (int n = 1; n <= 18; n++) { for (int k = 1; k <= 18; k++) { NCHOOSE[n][k] = NCHOOSE[n-1][k-1] + NCHOOSE[n-1][k]; } } } void fillResultsAndCounts237() { // Starting from one to ignore cases with only 3 and 7 which will be handled elsewhere for (int i = 1; i < 55; i++) { for (int j = 0; j < 37; j++) { for (int k = 0; k < 19; k++) { if (is237Valid(i, j, k)) { long long x = P2[i] * P3[j] * P7[k]; long long result = getResult(x); if (result != 0) { ND nd; nd.result= result; nd.count_2 = i; nd.count_3 = j; nd.count_5 = 0; nd.count_7 = k; nd.value = x; NON_ZERO.push_back(nd); } } } } } } void fillResultsAndCounts537() { for (int i = 0; i < 19; i++) { for (int j = 0; j < 37; j++) { for (int k = 0; k < 19; k++) { if (is537Valid(i, j, k)) { long long x = P5[i] * P3[j] * P7[k]; long long result = getResult(x); if (result != 0) { ND nd; nd.result= result; nd.count_2 = 0; nd.count_3 = j; nd.count_5 = i; nd.count_7 = k; nd.value = x; NON_ZERO.push_back(nd); } } } } } } void fillFD() { for (int i = 0; i < NON_ZERO.size(); i++) { ND data = NON_ZERO[i]; int max6s = min(data.count_2, data.count_3); int i5 = data.count_5; int i7 = data.count_7; for (int i6 = 0; i6 <= max6s; i6++) { int curr2s = data.count_2 - i6; int curr3s = data.count_3 - i6; int max8s = curr2s / 3; for (int i8 = 0; i8 <= max8s; i8++) { int curr2s_after8 = curr2s - i8 * 3; int max4s = curr2s_after8 / 2; for (int i4 = 0; i4 <= max4s; i4++) { int i2 = curr2s_after8 - i4 * 2; int max9s = curr3s / 2; for (int i9 = 0; i9 <= max9s; i9++) { int i3 = curr3s - i9 * 2; if (i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 > 18) { continue; } FD fd; fd.dc[2] = i2; fd.dc[3] = i3; fd.dc[4] = i4; fd.dc[5] = i5; fd.dc[6] = i6; fd.dc[7] = i7; fd.dc[8] = i8; fd.dc[9] = i9; fd.result = data.result; fd.value = data.value; fd.minDigits = max(i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9, 1); FD_NZ.push_back(fd); } } } } } } vector<long long> counts(long long n, bool withCache) { vector<long long> cts(10, 0); int numDigitsN = numDigits(n); if (withCache) { int i = numDigitsN - 1; for (int j = 1; j < 10; j++) { cts[j] = RESULT_CACHE_P10[i][j]; } } if (n >= MAXN) { for (int i = 0; i < 10; i++) { cts[i] = MAXN_RESULT[i]; } return cts; } for (int i = 0; i < FD_NZ.size(); i++) { int result = FD_NZ[i].result; if (result <= 0 || result > 9) { //cout<< "Invalid result: " << result; continue; } long long ct = FD_NZ[i].countLessThanN(n, numDigitsN, withCache); cts[result] += ct; } long long nonZero = 0LL; for (int i = 1; i < 10; i++) { nonZero += cts[i]; } cts[0] = n - nonZero; return cts; } void fillResultCache() { for (int i = 1; i <= 18; i++) { vector<long long> cts = counts(P10[i], false); for (int j = 0; j < 10; j++) { RESULT_CACHE_P10[i][j] = cts[j]; } } } void preprocessing() { fillPowers(); fillNChoose(); fillResultsAndCounts237(); fillResultsAndCounts537(); sort(NON_ZERO.begin(), NON_ZERO.end(), comp); fillFD(); fillResultCache(); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); preprocessing(); int t; cin >> t; for (int i = 0; i < t; i++) { long long n; cin >> n; vector<long long> results = counts(n, true); for (int j = 0; j < 10; j++) { cout << results[j] << " "; } 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 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 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | #include<iostream> #include<vector> #include<algorithm> #include <stdio.h> using namespace std; long long TWO = 2LL; long long THREE = 3LL; long long FIVE = 5LL; long long SEVEN = 7LL; long long TEN = 10LL; long long MAXN = 1000LL * 1000LL * 1000LL * 1000LL * 1000LL * 1000LL; long long MAXN_RESULT[10] = {999879067589400316LL, 18LL, 2795912956450LL, 171LL, 28611339267816LL, 78039555LL, 55909713312571LL, 171LL, 33615367021792LL, 1140LL}; long long P2[55]; long long P3[37]; long long P5[19]; long long P7[19]; long long P10[19]; long long RESULT_CACHE_P10[19][10]; long long getResult(long long n) { if (n >= 0 && n <= 9) { return n; } long long result = 1; while (n > 0) { long long digit = n % 10; if (digit == 0) { return 0; } else { n = n / 10LL; result = result * digit; } } return getResult(result); } long long NCHOOSE[19][19]; vector<int> digits(10); int numDigits(long long n) { for (int i = 17; i >= 0; i--) { if (P10[i] <= n) { return i + 1; } } return 0; } struct ND { int result; int count_2; int count_3; int count_5; int count_7; long long value; }; struct FD { // digit count int dc[10]; long long value; int result; int minDigits; void print() { for (int i = 2; i < 10; i++) { cout << i << ": " << dc[i] << ", "; } cout << "res: " << result << ", " << "val: " << value << endl; } int minNumDigits() { return minDigits; } long long getCount(vector<int>& digits) { long long product = 1LL; int topPart = 0; for (int i = 1; i < 10; i++) { topPart += digits[i]; } for (int i = 1; i < 10; i++) { product *= NCHOOSE[topPart][digits[i]]; topPart -= digits[i]; } return product; } long long countLessThanN(long long n, int numDigitsN, bool withCache) { if (value == 1LL) { int extra = withCache ? 1 : numDigitsN; for (int power = numDigitsN - 1; power >= 0; power--) { long long currentDigit = n / P10[power]; if (currentDigit > 1LL) { return extra; } else if (currentDigit < 1LL) { return extra - 1; } n -= currentDigit * P10[power]; } return extra; } if (n > P10[18]) { return 0LL; } if (minDigits > numDigitsN) { return 0LL; } long long count = 0LL; //cout<< "Value: " << value << endl; if (withCache && minDigits < numDigitsN) { long long product = 1LL; long long topPart = minDigits; for (int i = 2; i < 10; i++) { product *= NCHOOSE[topPart][dc[i]]; topPart -= dc[i]; } //cout<< "Product: " << product << endl; long long choice1 = 0LL; for (int i = minDigits; i < numDigitsN; i++) { choice1 += NCHOOSE[i][minDigits]; //cout<< "i: " << i << ", choice1 = " << choice1 << endl; } //cout<< "Choice1: " << choice1 << endl; count = product * choice1; } //cout<< "Count: " << count << endl; if (value == 1LL) { dc[1] = numDigitsN; } else { dc[1] = numDigitsN - minDigits; } for (int i = 1; i < 10; i++) { digits[i] = dc[i]; //cout<< i << ": " << digits[i] << ", "; } //cout<< "\n"; for (int i = numDigitsN - 1; i >= 0; i--) { long long power = P10[i]; //cout<< "Power: " << power << endl; long long currentDigit = n / power; //cout<< "Current digit: " << currentDigit << endl; if (currentDigit == 0) { return count; } for (int j = 1; j < currentDigit || (i == 0 && j == currentDigit); j++) { //cout<< "j: " << j << endl; if (digits[j] == 0) { continue; } else { digits[j] -= 1; long long newCount = getCount(digits); //cout<< "Extra count: " << newCount << endl; count += newCount; digits[j] += 1; } } if (digits[currentDigit] == 0) { return count; } digits[currentDigit] -= 1; n -= currentDigit * power; } return count; } }; bool comp(const ND& nd1, const ND& nd2) { return nd1.value < nd2.value; } bool is237Valid(int x2, int x3, int x7) { return x2 / 3 + x3 / 2 + x7 <= 18; } bool is537Valid(int x5, int x3, int x7) { return x5 + x3 / 2 + x7 <= 18; } vector<ND> NON_ZERO; vector<FD> FD_NZ; void fillPowers() { P2[0] = 1LL; P3[0] = 1LL; P5[0] = 1LL; P7[0] = 1LL; P10[0] = 1LL; for (int i = 1; i < 55; i++) { P2[i] = P2[i-1] * TWO; } for (int i = 1; i < 37; i++) { P3[i] = P3[i-1] * THREE; } for (int i = 1; i < 19; i++) { P5[i] = P5[i-1] * FIVE; P7[i] = P7[i-1] * SEVEN; P10[i] = P10[i-1] * TEN; } } void fillNChoose() { for (int i = 0; i <= 18; i++) { NCHOOSE[i][0] = 1LL; } for (int j = 1; j <= 18; j++) { NCHOOSE[0][j] = 0; } for (int n = 1; n <= 18; n++) { for (int k = 1; k <= 18; k++) { NCHOOSE[n][k] = NCHOOSE[n-1][k-1] + NCHOOSE[n-1][k]; } } } void fillResultsAndCounts237() { // Starting from one to ignore cases with only 3 and 7 which will be handled elsewhere for (int i = 1; i < 55; i++) { for (int j = 0; j < 37; j++) { for (int k = 0; k < 19; k++) { if (is237Valid(i, j, k)) { long long x = P2[i] * P3[j] * P7[k]; long long result = getResult(x); if (result != 0) { ND nd; nd.result= result; nd.count_2 = i; nd.count_3 = j; nd.count_5 = 0; nd.count_7 = k; nd.value = x; NON_ZERO.push_back(nd); } } } } } } void fillResultsAndCounts537() { for (int i = 0; i < 19; i++) { for (int j = 0; j < 37; j++) { for (int k = 0; k < 19; k++) { if (is537Valid(i, j, k)) { long long x = P5[i] * P3[j] * P7[k]; long long result = getResult(x); if (result != 0) { ND nd; nd.result= result; nd.count_2 = 0; nd.count_3 = j; nd.count_5 = i; nd.count_7 = k; nd.value = x; NON_ZERO.push_back(nd); } } } } } } void fillFD() { for (int i = 0; i < NON_ZERO.size(); i++) { ND data = NON_ZERO[i]; int max6s = min(data.count_2, data.count_3); int i5 = data.count_5; int i7 = data.count_7; for (int i6 = 0; i6 <= max6s; i6++) { int curr2s = data.count_2 - i6; int curr3s = data.count_3 - i6; int max8s = curr2s / 3; for (int i8 = 0; i8 <= max8s; i8++) { int curr2s_after8 = curr2s - i8 * 3; int max4s = curr2s_after8 / 2; for (int i4 = 0; i4 <= max4s; i4++) { int i2 = curr2s_after8 - i4 * 2; int max9s = curr3s / 2; for (int i9 = 0; i9 <= max9s; i9++) { int i3 = curr3s - i9 * 2; if (i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9 > 18) { continue; } FD fd; fd.dc[2] = i2; fd.dc[3] = i3; fd.dc[4] = i4; fd.dc[5] = i5; fd.dc[6] = i6; fd.dc[7] = i7; fd.dc[8] = i8; fd.dc[9] = i9; fd.result = data.result; fd.value = data.value; fd.minDigits = max(i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9, 1); FD_NZ.push_back(fd); } } } } } } vector<long long> counts(long long n, bool withCache) { vector<long long> cts(10, 0); int numDigitsN = numDigits(n); if (withCache) { int i = numDigitsN - 1; for (int j = 1; j < 10; j++) { cts[j] = RESULT_CACHE_P10[i][j]; } } if (n >= MAXN) { for (int i = 0; i < 10; i++) { cts[i] = MAXN_RESULT[i]; } return cts; } for (int i = 0; i < FD_NZ.size(); i++) { int result = FD_NZ[i].result; if (result <= 0 || result > 9) { //cout<< "Invalid result: " << result; continue; } long long ct = FD_NZ[i].countLessThanN(n, numDigitsN, withCache); cts[result] += ct; } long long nonZero = 0LL; for (int i = 1; i < 10; i++) { nonZero += cts[i]; } cts[0] = n - nonZero; return cts; } void fillResultCache() { for (int i = 1; i <= 18; i++) { vector<long long> cts = counts(P10[i], false); for (int j = 0; j < 10; j++) { RESULT_CACHE_P10[i][j] = cts[j]; } } } void preprocessing() { fillPowers(); fillNChoose(); fillResultsAndCounts237(); fillResultsAndCounts537(); sort(NON_ZERO.begin(), NON_ZERO.end(), comp); fillFD(); fillResultCache(); } int main() { std::ios::sync_with_stdio(false); std::cin.tie(NULL); preprocessing(); int t; cin >> t; for (int i = 0; i < t; i++) { long long n; cin >> n; vector<long long> results = counts(n, true); for (int j = 0; j < 10; j++) { cout << results[j] << " "; } cout << "\n"; } } |