#include <algorithm> #include <cstdio> #include <map> #include <tuple> #include <vector> using namespace std; using counts_type = vector<unsigned long long>; using digits_type = vector<short>; const int MAX_NUMBER_OF_DIGITS = 19; template <typename T> void print_vector(vector<T> v) { for (int i = 0; i < v.size(); i++) { printf("%lld ", v[i]); } puts(""); } map<unsigned long long, counts_type> cache[MAX_NUMBER_OF_DIGITS][2][2]; digits_type number_to_digits(unsigned long long n) { digits_type digits(MAX_NUMBER_OF_DIGITS, 0); int idx = digits.size() - 1; while (n) { short last_digit = n % 10; digits[idx--] = last_digit; n = n / 10; } return digits; } int get_final_digit(unsigned long long n) { if (n < 10) { return n; } unsigned long long product = 1; while (n > 0) { short digit = n % 10; if (digit == 0) { return 0; } product = product * digit; n = n / 10; } // printf("%llu\n", product); return get_final_digit(product); } counts_type count_digits(short pos, bool tight, bool number_started, unsigned long long product, digits_type &digits) { if (pos == digits.size()) { if (!number_started) { return {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; } counts_type result(10); result[get_final_digit(product)]++; return result; } if (cache[pos][tight][number_started].contains(product)) { return cache[pos][tight][number_started][product]; } counts_type result(10); short limit = tight ? digits[pos] : 9; for (int i = 0; i <= limit; i++) { bool next_tight = tight && i == limit; bool next_number_started = number_started || i > 0; unsigned long long next_product = number_started ? product * i : i; if (i == 0) { if (!number_started) { next_product = product; } else { next_product = 0; } } counts_type partial_sum = count_digits( pos + 1, next_tight, next_number_started, next_product, digits); for (int j = 0; j < partial_sum.size(); j++) { result[j] += partial_sum[j]; } } cache[pos][tight][number_started].insert({product, result}); return result; } counts_type solve(digits_type digits) { counts_type result; for (int i=0; i<MAX_NUMBER_OF_DIGITS; i++) { for (int j=0; j<2; j++) { cache[i][1][j].clear(); } } result = count_digits(0, true, false, 1, digits); result[0]--; return result; } int main() { int t; scanf("%d", &t); // print_vector(solve(number_to_digits(56))); // print_vector(solve(number_to_digits(57))); for (int i = 0; i < t; i++) { unsigned long long n; scanf("%llu", &n); auto result = solve(number_to_digits(n)); print_vector(result); } return 0; }
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 | #include <algorithm> #include <cstdio> #include <map> #include <tuple> #include <vector> using namespace std; using counts_type = vector<unsigned long long>; using digits_type = vector<short>; const int MAX_NUMBER_OF_DIGITS = 19; template <typename T> void print_vector(vector<T> v) { for (int i = 0; i < v.size(); i++) { printf("%lld ", v[i]); } puts(""); } map<unsigned long long, counts_type> cache[MAX_NUMBER_OF_DIGITS][2][2]; digits_type number_to_digits(unsigned long long n) { digits_type digits(MAX_NUMBER_OF_DIGITS, 0); int idx = digits.size() - 1; while (n) { short last_digit = n % 10; digits[idx--] = last_digit; n = n / 10; } return digits; } int get_final_digit(unsigned long long n) { if (n < 10) { return n; } unsigned long long product = 1; while (n > 0) { short digit = n % 10; if (digit == 0) { return 0; } product = product * digit; n = n / 10; } // printf("%llu\n", product); return get_final_digit(product); } counts_type count_digits(short pos, bool tight, bool number_started, unsigned long long product, digits_type &digits) { if (pos == digits.size()) { if (!number_started) { return {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; } counts_type result(10); result[get_final_digit(product)]++; return result; } if (cache[pos][tight][number_started].contains(product)) { return cache[pos][tight][number_started][product]; } counts_type result(10); short limit = tight ? digits[pos] : 9; for (int i = 0; i <= limit; i++) { bool next_tight = tight && i == limit; bool next_number_started = number_started || i > 0; unsigned long long next_product = number_started ? product * i : i; if (i == 0) { if (!number_started) { next_product = product; } else { next_product = 0; } } counts_type partial_sum = count_digits( pos + 1, next_tight, next_number_started, next_product, digits); for (int j = 0; j < partial_sum.size(); j++) { result[j] += partial_sum[j]; } } cache[pos][tight][number_started].insert({product, result}); return result; } counts_type solve(digits_type digits) { counts_type result; for (int i=0; i<MAX_NUMBER_OF_DIGITS; i++) { for (int j=0; j<2; j++) { cache[i][1][j].clear(); } } result = count_digits(0, true, false, 1, digits); result[0]--; return result; } int main() { int t; scanf("%d", &t); // print_vector(solve(number_to_digits(56))); // print_vector(solve(number_to_digits(57))); for (int i = 0; i < t; i++) { unsigned long long n; scanf("%llu", &n); auto result = solve(number_to_digits(n)); print_vector(result); } return 0; } |