#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<vector<long long>> bnms;
map<long long, vector<vector<long long>>> precompute;
int compute(long long a) {
if (a < 10) return a;
long long tmp = 1;
while (a) {
tmp *= a%10;
a /= 10;
}
return compute(tmp);
}
long long binom(int a, int b) {
if (a < 0 || b < 0 || b > a) return 0;
if (bnms[a][b] != -1) return bnms[a][b];
long long num=1, den=1;
for (int i=a; i>=a-b+1; i--) num *= i;
for (int i=2; i<=b; i++) den *= i;
return bnms[a][b] = num/den;
}
long long choose(int two, int three, int five, int seven, int l) {
long long res = 0;
long long mlp = 1;
mlp *= binom(l, five);
l -= five;
if (l < 0) return 0;
mlp *= binom(l, seven);
l -= seven;
if (l < 0) return 0;
for (int six=0; six<=min(three,two); six++) {
int tmp1 = l;
if (tmp1-six < 0) break;
long long place6 = 1;
place6 *= binom(tmp1, six);
tmp1 -= six;
for (int nine=0; nine<=(three-six)/2; nine++) {
int tmp2 = tmp1;
if (tmp2-nine-(three-2*nine-six) < 0) continue;
long long place3 = 1;
place3 *= binom(tmp2, nine);
tmp2 -= nine;
place3 *= binom(tmp2, three-2*nine-six);
tmp2 -= three-2*nine-six;
for (int eight=0; eight<=(two-six)/3; eight++) for (int four=0; four<=(two-six-3*eight)/2; four++) {
int tmp3 = tmp2;
if (tmp3-eight-four-(two-3*eight-2*four-six) < 0) continue;
long long place2 = 1;
place2 *= binom(tmp3, eight);
tmp3 -= eight;
place2 *= binom(tmp3, four);
tmp3 -= four;
place2 *= binom(tmp3, two-3*eight-2*four-six);
res += mlp*place6*place3*place2;
}
}
}
return res;
}
long long to_ll(int two, int three, int five, int seven) {
long long a = 1;
while (two--) a *= 2;
while (three--) a *= 3;
while (five--) a *= 5;
while (seven--) a *= 7;
return a;
}
long long calc(long long num, string a, bool rec, int beg) {
auto it = precompute.find(num);
if (a.size() == beg+1) return it->second[0][a[beg]-'1'];
long long res = it->second[a.size()-beg-1][a[beg]-'1'];
if (!rec) for (int i=a.size()-beg-2; i>=0; i--) res += it->second[i].back();
if (a[beg+1] == '0') return res;
if (num%(a[beg]-'0') == 0) res += calc(num/(a[beg]-'0'), a, true, beg+1);
return res;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
bnms = vector(19,vector<long long>(19,-1));
vector<tuple<int,int,int,int>> dests;
long long mx = 1e18;
long long twos = 1;
for (int i=0;;i++) {
if (twos > mx) break;
long long threes = 1;
for (int j=0;;j++) {
if (twos*threes > mx) break;
long long fives = 1;
for (int k=0;;k++) {
if (twos > 1 && fives > 1) break;
if (twos*threes*fives > mx) break;
long long sevens = 1;
for (int l=0;;l++) {
if (twos*threes*fives*sevens > mx) break;
dests.push_back({i,j,k,l});
sevens *= 7;
}
fives *= 5;
}
threes *= 3;
}
twos *= 2;
}
for (int i=0; i<dests.size(); i++) {
auto [two,three,five,seven] = dests[i];
long long vl = to_ll(two,three,five,seven);
precompute.insert({vl, vector<vector<long long>>(19, vector<long long>(10))});
auto it = precompute.find(vl);
for (int l=0; l<19; l++) for (int d=0; d<10; d++) {
if (l==0) {
it->second[l][d] = 0;
if (vl == d+1) it->second[l][d] += 1;
if (d) it->second[l][d] += it->second[l][d-1];
} else {
if (d==0) it->second[l][d] = 0;
else {
it->second[l][d] = it->second[l][d-1];
if (d==1) it->second[l][d] += choose(two, three, five, seven, l);
else if (d==2 && two>0) it->second[l][d] += choose(two-1,three,five,seven,l);
else if (d==3 && three>0) it->second[l][d] += choose(two, three-1, five, seven, l);
else if (d==4 && two>1) it->second[l][d] += choose(two-2, three, five, seven, l);
else if (d==5 && five>0) it->second[l][d] += choose(two, three, five-1, seven, l);
else if (d==6 && two>0 && three>0) it->second[l][d] += choose(two-1, three-1, five, seven, l);
else if (d==7 && seven>0) it->second[l][d] += choose(two, three, five, seven-1, l);
else if (d==8 && two>2) it->second[l][d] += choose(two-3, three, five, seven, l);
else if (d==9 && three>1) it->second[l][d] += choose(two, three-2, five, seven, l);
}
}
}
}
vector<int> results(dests.size());
for (int i=0; i<dests.size(); i++) {
auto [two,three,five,seven] = dests[i];
long long a = to_ll(two,three,five,seven);
results[i] = compute(a);
}
vector<tuple<int,int,int,int>> dests2;
vector<int> results2;
for (int i=0; i<dests.size(); i++) if (results[i]) {
dests2.push_back(dests[i]);
results2.push_back(results[i]);
}
dests = dests2;
results = results2;
/* for (auto[two,three,five,seven] : dests) { */
/* long long a = to_ll(two,three,five,seven); */
/* long long r = calc(a,to_string(123),false); */
/* if (r) { */
/* cout<<a<<" -> "<<r<<'\n'; */
/* } */
/* } */
/* cout<<calc(1,"123"); */
/* cout<<choose(1,1,0,0,1); */
int t;
cin>>t;
while (t--) {
long long n;
cin>>n;
vector<long long> res(10);
for (int i=0; i<dests.size(); i++) {
auto [two,three,five,seven] = dests[i];
res[results[i]] += calc(to_ll(two,three,five,seven), to_string(n), false, 0);
}
long long sm = 0;
for (long long s : res) sm += s;
res[0] += n-sm;
for (int i=0; i<10; i++) cout<<res[i]<<' ';
cout<<'\n';
}
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 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 | #include <iostream> #include <string> #include <vector> #include <map> using namespace std; vector<vector<long long>> bnms; map<long long, vector<vector<long long>>> precompute; int compute(long long a) { if (a < 10) return a; long long tmp = 1; while (a) { tmp *= a%10; a /= 10; } return compute(tmp); } long long binom(int a, int b) { if (a < 0 || b < 0 || b > a) return 0; if (bnms[a][b] != -1) return bnms[a][b]; long long num=1, den=1; for (int i=a; i>=a-b+1; i--) num *= i; for (int i=2; i<=b; i++) den *= i; return bnms[a][b] = num/den; } long long choose(int two, int three, int five, int seven, int l) { long long res = 0; long long mlp = 1; mlp *= binom(l, five); l -= five; if (l < 0) return 0; mlp *= binom(l, seven); l -= seven; if (l < 0) return 0; for (int six=0; six<=min(three,two); six++) { int tmp1 = l; if (tmp1-six < 0) break; long long place6 = 1; place6 *= binom(tmp1, six); tmp1 -= six; for (int nine=0; nine<=(three-six)/2; nine++) { int tmp2 = tmp1; if (tmp2-nine-(three-2*nine-six) < 0) continue; long long place3 = 1; place3 *= binom(tmp2, nine); tmp2 -= nine; place3 *= binom(tmp2, three-2*nine-six); tmp2 -= three-2*nine-six; for (int eight=0; eight<=(two-six)/3; eight++) for (int four=0; four<=(two-six-3*eight)/2; four++) { int tmp3 = tmp2; if (tmp3-eight-four-(two-3*eight-2*four-six) < 0) continue; long long place2 = 1; place2 *= binom(tmp3, eight); tmp3 -= eight; place2 *= binom(tmp3, four); tmp3 -= four; place2 *= binom(tmp3, two-3*eight-2*four-six); res += mlp*place6*place3*place2; } } } return res; } long long to_ll(int two, int three, int five, int seven) { long long a = 1; while (two--) a *= 2; while (three--) a *= 3; while (five--) a *= 5; while (seven--) a *= 7; return a; } long long calc(long long num, string a, bool rec, int beg) { auto it = precompute.find(num); if (a.size() == beg+1) return it->second[0][a[beg]-'1']; long long res = it->second[a.size()-beg-1][a[beg]-'1']; if (!rec) for (int i=a.size()-beg-2; i>=0; i--) res += it->second[i].back(); if (a[beg+1] == '0') return res; if (num%(a[beg]-'0') == 0) res += calc(num/(a[beg]-'0'), a, true, beg+1); return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); bnms = vector(19,vector<long long>(19,-1)); vector<tuple<int,int,int,int>> dests; long long mx = 1e18; long long twos = 1; for (int i=0;;i++) { if (twos > mx) break; long long threes = 1; for (int j=0;;j++) { if (twos*threes > mx) break; long long fives = 1; for (int k=0;;k++) { if (twos > 1 && fives > 1) break; if (twos*threes*fives > mx) break; long long sevens = 1; for (int l=0;;l++) { if (twos*threes*fives*sevens > mx) break; dests.push_back({i,j,k,l}); sevens *= 7; } fives *= 5; } threes *= 3; } twos *= 2; } for (int i=0; i<dests.size(); i++) { auto [two,three,five,seven] = dests[i]; long long vl = to_ll(two,three,five,seven); precompute.insert({vl, vector<vector<long long>>(19, vector<long long>(10))}); auto it = precompute.find(vl); for (int l=0; l<19; l++) for (int d=0; d<10; d++) { if (l==0) { it->second[l][d] = 0; if (vl == d+1) it->second[l][d] += 1; if (d) it->second[l][d] += it->second[l][d-1]; } else { if (d==0) it->second[l][d] = 0; else { it->second[l][d] = it->second[l][d-1]; if (d==1) it->second[l][d] += choose(two, three, five, seven, l); else if (d==2 && two>0) it->second[l][d] += choose(two-1,three,five,seven,l); else if (d==3 && three>0) it->second[l][d] += choose(two, three-1, five, seven, l); else if (d==4 && two>1) it->second[l][d] += choose(two-2, three, five, seven, l); else if (d==5 && five>0) it->second[l][d] += choose(two, three, five-1, seven, l); else if (d==6 && two>0 && three>0) it->second[l][d] += choose(two-1, three-1, five, seven, l); else if (d==7 && seven>0) it->second[l][d] += choose(two, three, five, seven-1, l); else if (d==8 && two>2) it->second[l][d] += choose(two-3, three, five, seven, l); else if (d==9 && three>1) it->second[l][d] += choose(two, three-2, five, seven, l); } } } } vector<int> results(dests.size()); for (int i=0; i<dests.size(); i++) { auto [two,three,five,seven] = dests[i]; long long a = to_ll(two,three,five,seven); results[i] = compute(a); } vector<tuple<int,int,int,int>> dests2; vector<int> results2; for (int i=0; i<dests.size(); i++) if (results[i]) { dests2.push_back(dests[i]); results2.push_back(results[i]); } dests = dests2; results = results2; /* for (auto[two,three,five,seven] : dests) { */ /* long long a = to_ll(two,three,five,seven); */ /* long long r = calc(a,to_string(123),false); */ /* if (r) { */ /* cout<<a<<" -> "<<r<<'\n'; */ /* } */ /* } */ /* cout<<calc(1,"123"); */ /* cout<<choose(1,1,0,0,1); */ int t; cin>>t; while (t--) { long long n; cin>>n; vector<long long> res(10); for (int i=0; i<dests.size(); i++) { auto [two,three,five,seven] = dests[i]; res[results[i]] += calc(to_ll(two,three,five,seven), to_string(n), false, 0); } long long sm = 0; for (long long s : res) sm += s; res[0] += n-sm; for (int i=0; i<10; i++) cout<<res[i]<<' '; cout<<'\n'; } return 0; } |
English