#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
string s;
LL calculate_MDR(LL x)
{
if (x < 10)
return x;
LL y = 1;
while (x > 0)
{
y *= (x%10);
x /= 10;
}
return calculate_MDR(y);
}
struct State {
int pos;
int tight;
int started;
LL prod;
bool operator==(const State &other) const
{
return pos == other.pos && tight == other.tight &&
started == other.started && prod == other.prod;
}
};
struct StateHash {
size_t operator()(const State &st) const
{
size_t res = 59;
res = res * 31 + hash<int>()(st.pos);
res = res * 31 + hash<int>()(st.tight);
res = res * 31 + hash<int>()(st.started);
res = res * 31 + hash<LL>()(st.prod);
return res;
}
};
unordered_map<State, LL, StateHash> memo;
LL solveDP(int pos, int tight, int started, LL prod, int digit)
{
if(pos == s.size())
{
if(!started)
return 0;
return (calculate_MDR(prod) == digit);
}
State st {pos, tight, started, prod};
if(memo.find(st) != memo.end())
return memo[st];
LL res = 0;
int limit = (tight ? s[pos] - '0' : 9);
for (int d = 0; d <= limit; d++)
{
int ntight = (tight && d == limit);
int nstarted = started;
LL nprod = prod;
if(!started)
{
if(d == 0)
{
nprod = 1;
}
else
{
nstarted = 1;
nprod = d;
}
}
else
{
if(d == 0)
nprod = 0;
else
nprod = prod * d;
}
res += solveDP(pos + 1, ntight, nstarted, nprod, digit);
}
memo[st] = res;
return res;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
int tt;
cin >> tt;
while (tt--)
{
LL n;
cin >> n;
s = to_string(n);
for (int d = 0; d <= 9; d++)
{
memo.clear();
cout << solveDP(0, 1, 0, 1, d) << " ";
}
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 | #include <bits/stdc++.h> using namespace std; typedef long long LL; string s; LL calculate_MDR(LL x) { if (x < 10) return x; LL y = 1; while (x > 0) { y *= (x%10); x /= 10; } return calculate_MDR(y); } struct State { int pos; int tight; int started; LL prod; bool operator==(const State &other) const { return pos == other.pos && tight == other.tight && started == other.started && prod == other.prod; } }; struct StateHash { size_t operator()(const State &st) const { size_t res = 59; res = res * 31 + hash<int>()(st.pos); res = res * 31 + hash<int>()(st.tight); res = res * 31 + hash<int>()(st.started); res = res * 31 + hash<LL>()(st.prod); return res; } }; unordered_map<State, LL, StateHash> memo; LL solveDP(int pos, int tight, int started, LL prod, int digit) { if(pos == s.size()) { if(!started) return 0; return (calculate_MDR(prod) == digit); } State st {pos, tight, started, prod}; if(memo.find(st) != memo.end()) return memo[st]; LL res = 0; int limit = (tight ? s[pos] - '0' : 9); for (int d = 0; d <= limit; d++) { int ntight = (tight && d == limit); int nstarted = started; LL nprod = prod; if(!started) { if(d == 0) { nprod = 1; } else { nstarted = 1; nprod = d; } } else { if(d == 0) nprod = 0; else nprod = prod * d; } res += solveDP(pos + 1, ntight, nstarted, nprod, digit); } memo[st] = res; return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tt; cin >> tt; while (tt--) { LL n; cin >> n; s = to_string(n); for (int d = 0; d <= 9; d++) { memo.clear(); cout << solveDP(0, 1, 0, 1, d) << " "; } cout << "\n"; } } |
English