#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cassert>
#include <cmath>
using namespace std;
long long reduce_number(long long x){
if(x < 10){
return x;
}
long long ans = 1;
while(x >= 10){
ans *= (x % 10);
x /= 10;
}
return reduce_number(ans * x);
}
vector<vector<long long>> results; // contains results in intervals [1, 10^i - 1]
// results[i] daje wynik dla liczb z przedziału [1, 10^(i+1) - 1]
map<long long, long long> basic_map = {
{1,1},
{2,1},
{3,1},
{4,1},
{5,1},
{6,1},
{7,1},
{8,1},
{9,1}
};
map<long long, long long> new_map;
void prefix_sums(){
for(int i=1; i<results.size(); i++){
for(int j=0; j<10; j++){
results[i][j] += results[i - 1][j];
}
}
}
map<long long, long long> all_products[20];
void fill_all_products(){
all_products[1] = {
{1, 1},
{2, 1},
{3, 1},
{4, 1},
{5, 1},
{6, 1},
{7, 1},
{8, 1},
{9, 1},
{0, 1}
};
for(int i=2; i<=18; i++){
for(long long ost = 0; ost <= 9; ost++){
for(auto it = all_products[i - 1].begin(); it != all_products[i - 1].end(); it++){
long long iloczyn = it->first * ost;
all_products[i][iloczyn] += it->second;
}
}
}
}
vector<long long> get_digits(long long n){
vector<long long> digits;
while(n > 0){
digits.push_back(n % 10);
n /= 10;
}
reverse(digits.begin(), digits.end());
return digits;
}
vector<long long> answer;
vector<long long> add_vectors(const vector<long long>& a, const vector<long long>& b) {
vector<long long> result(a.size());
for (size_t i = 0; i < a.size(); ++i) {
result[i] = a[i] + b[i];
}
return result;
}
long long suma(){
long long sum = 0;
for(long long x: answer){
sum += x;
}
return sum;
}
void handle(long long n){
vector<long long> digits = get_digits(n);
vector<long long> digits_prod = get_digits(n);
for(int i = 1; i < digits.size(); i++){
digits_prod[i] *= digits_prod[i - 1];
}
reverse(digits_prod.begin(), digits_prod.end());
reverse(digits.begin(), digits.end());
for(int i=0; i<digits.size(); i++){
if(i == 0){
for(long long digit = 0; digit <= digits[i]; digit++){
long long product = digit * digits_prod[i+1];
answer[reduce_number(product)] += 1;
}
// assert(suma() == 8);
}
else {
long long digit = 0;
if(i == digits.size() - 1) digit = 1;
for(; digit < digits[i]; digit++){
long long product = digit;
if(i != digits.size() - 1) product *= digits_prod[i+1];
for(auto it = all_products[i].begin(); it != all_products[i].end(); it++){
long long new_product = it->first * product;
answer[reduce_number(new_product)] += it->second;
}
}
// assert(suma() == 48);
}
}
answer = add_vectors(answer, results[digits.size() - 2]);
// assert(suma() == 57);
// answer += results[digits.size() - 2];
}
int main(){
results.push_back({0, 1, 1, 1, 1, 1, 1, 1, 1, 1}); //[1-9]
for(int ile_cyfr=2; ile_cyfr<=18; ile_cyfr++){
for(long long ost = 0; ost <= 9; ost++){
for(auto it = basic_map.begin(); it != basic_map.end(); it++){
long long iloczyn = it->first * ost;
new_map[iloczyn] += it->second;
}
}
vector<long long> res(10, 0);
for(auto it = new_map.begin(); it != new_map.end(); it++){
res[reduce_number(it->first)] += it->second;
}
results.push_back(res);
basic_map = new_map;
new_map.clear();
}
prefix_sums();
// for(int i=0; i<results.size(); i++){
// long long sum = 0;
// for(int j=0; j<10; j++){
// // cout<<results[i][j]<<" ";
// sum += results[i][j];
// }
// assert(sum == 10*pow(10, i)-1);
// cout<<endl;
// }
fill_all_products();
int t;
cin >> t;
while(t--){
long long n;
cin >> n;
answer.clear();
for(int i=0; i<10; i++){
answer.push_back(0);
}
if(n < 10){
for(int i=1; i<=n; i++){
answer[i] = 1;
}
for(int i=0; i<10; i++){
cout<<answer[i]<<" ";
}
cout<<endl;
continue;
}
handle(n);
for(int i=0; i<10; i++){
cout<<answer[i]<<" ";
}
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 | #include <iostream> #include <string> #include <vector> #include <algorithm> #include <map> #include <cassert> #include <cmath> using namespace std; long long reduce_number(long long x){ if(x < 10){ return x; } long long ans = 1; while(x >= 10){ ans *= (x % 10); x /= 10; } return reduce_number(ans * x); } vector<vector<long long>> results; // contains results in intervals [1, 10^i - 1] // results[i] daje wynik dla liczb z przedziału [1, 10^(i+1) - 1] map<long long, long long> basic_map = { {1,1}, {2,1}, {3,1}, {4,1}, {5,1}, {6,1}, {7,1}, {8,1}, {9,1} }; map<long long, long long> new_map; void prefix_sums(){ for(int i=1; i<results.size(); i++){ for(int j=0; j<10; j++){ results[i][j] += results[i - 1][j]; } } } map<long long, long long> all_products[20]; void fill_all_products(){ all_products[1] = { {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1}, {9, 1}, {0, 1} }; for(int i=2; i<=18; i++){ for(long long ost = 0; ost <= 9; ost++){ for(auto it = all_products[i - 1].begin(); it != all_products[i - 1].end(); it++){ long long iloczyn = it->first * ost; all_products[i][iloczyn] += it->second; } } } } vector<long long> get_digits(long long n){ vector<long long> digits; while(n > 0){ digits.push_back(n % 10); n /= 10; } reverse(digits.begin(), digits.end()); return digits; } vector<long long> answer; vector<long long> add_vectors(const vector<long long>& a, const vector<long long>& b) { vector<long long> result(a.size()); for (size_t i = 0; i < a.size(); ++i) { result[i] = a[i] + b[i]; } return result; } long long suma(){ long long sum = 0; for(long long x: answer){ sum += x; } return sum; } void handle(long long n){ vector<long long> digits = get_digits(n); vector<long long> digits_prod = get_digits(n); for(int i = 1; i < digits.size(); i++){ digits_prod[i] *= digits_prod[i - 1]; } reverse(digits_prod.begin(), digits_prod.end()); reverse(digits.begin(), digits.end()); for(int i=0; i<digits.size(); i++){ if(i == 0){ for(long long digit = 0; digit <= digits[i]; digit++){ long long product = digit * digits_prod[i+1]; answer[reduce_number(product)] += 1; } // assert(suma() == 8); } else { long long digit = 0; if(i == digits.size() - 1) digit = 1; for(; digit < digits[i]; digit++){ long long product = digit; if(i != digits.size() - 1) product *= digits_prod[i+1]; for(auto it = all_products[i].begin(); it != all_products[i].end(); it++){ long long new_product = it->first * product; answer[reduce_number(new_product)] += it->second; } } // assert(suma() == 48); } } answer = add_vectors(answer, results[digits.size() - 2]); // assert(suma() == 57); // answer += results[digits.size() - 2]; } int main(){ results.push_back({0, 1, 1, 1, 1, 1, 1, 1, 1, 1}); //[1-9] for(int ile_cyfr=2; ile_cyfr<=18; ile_cyfr++){ for(long long ost = 0; ost <= 9; ost++){ for(auto it = basic_map.begin(); it != basic_map.end(); it++){ long long iloczyn = it->first * ost; new_map[iloczyn] += it->second; } } vector<long long> res(10, 0); for(auto it = new_map.begin(); it != new_map.end(); it++){ res[reduce_number(it->first)] += it->second; } results.push_back(res); basic_map = new_map; new_map.clear(); } prefix_sums(); // for(int i=0; i<results.size(); i++){ // long long sum = 0; // for(int j=0; j<10; j++){ // // cout<<results[i][j]<<" "; // sum += results[i][j]; // } // assert(sum == 10*pow(10, i)-1); // cout<<endl; // } fill_all_products(); int t; cin >> t; while(t--){ long long n; cin >> n; answer.clear(); for(int i=0; i<10; i++){ answer.push_back(0); } if(n < 10){ for(int i=1; i<=n; i++){ answer[i] = 1; } for(int i=0; i<10; i++){ cout<<answer[i]<<" "; } cout<<endl; continue; } handle(n); for(int i=0; i<10; i++){ cout<<answer[i]<<" "; } cout<<endl; } } |
English