The problem
Lets $$$f(x) = $$$ product of digits of $$$x$$$. Example: $$$f(123) = 1 * 2 * 3 = 6$$$, $$$f(990) = 9 * 9 * 0 = 0$$$, $$$f(1) = 1$$$
The statement is, given such limitation $$$N$$$, count the number of positive $$$x$$$ that $$$1 \leq x * f(x) \leq N$$$
Example: For $$$N = 20$$$, there are 5 valid numbers $$$1, 2, 3, 4, 11$$$
The limitation
- Subtask 1: $$$N \leq 10^6$$$
- Subtask 2: $$$N \leq 10^{10}$$$
- Subtask 3: $$$N \leq 10^{14}$$$
- Subtask 4: $$$N \leq 10^{18}$$$
My approach for subtask 1
- If $$$(x > N)$$$ or $$$(f(x) > N)$$$ then $$$(x * f(x) > N)$$$. So we will only care about $$$x \leq N$$$ that $$$x * f(x) \leq N$$$
Calculating x * f(x) - O(log10(x))ll cal(ll x, ll lim)
{
ll res = x;
do {
int t = x % 10;
if (res > lim / t) return -1; /// (x * f(x) > N)
res *= t;
} while (x /= 10);
return res;
}
Counting - O(n log10(n))ll brute(ll n)
{
ll res = 0;
for (ll x = 1; x <= n; ++x)
res += cal(x, n) > 0; /// 1 <= x * f(x) <= N
return res;
}
My approach for subtask 2
- If $$$x$$$ contains $$$0$$$ then $$$f(x) = 0 \Rightarrow x * f(x) < 1$$$. We only care about such $$$x$$$ without $$$0$$$ digit
Building function - approx O(result)/// Run 2e13 under 1 secs in (-O2) flag
/// X: current X
/// Y: f(X)
/// T: X * f(X)
ll N;
int res = 0;
vector<int> d;
void build(ll X = 0, ll Y = 1, ll T = 0)
{
if (T >= 1) ++res; /// 1 <= x * f(x) <= N
for (int v = 1; v <= 9; ++v) /// if (v = 0) then f(x) = 0
{
ll NX = X * 10 + v; /// Insert rightmost digits
ll NY = Y * v; /// Calculate digits production
ll NT = NX * NY;
if (NT > N) break; /// x * f(x) > N
build(NX, NY, NT);
}
}
Here is the solution:
Let takes some $$$x$$$ satisfy $$$1 \leq x * f(x) \leq N$$$
We can easily prove that $$$f(x) \leq x$$$, and because $$$x * f(x) \leq N$$$, we have $$$f(x) \leq \sqrt{N}$$$ (notice that $$$x$$$ might bigger than $$$\sqrt{N}$$$)
Since $$$f(x)$$$ is product of digits of $$$x$$$, which can be obtain by such digits {$$$1, 2, \dots, 9$$$}. So $$$f(x) = 2^a \times 3^b \times 5^c \times 7^d$$$
So we can bruteforces all possible tuple of such $$$(a, b, c, d)$$$ satisfy ($$$P = 2^a \times 3^b \times 5^c \times 7^d \leq \sqrt{N}$$$). There are about approximately $$$O(\sqrt[4]{N})$$$ such tuples (493 for $$$N = 10^9$$$ and 5914 tuples for $$$N = 10^{18}$$$)
Find all possible tuples - O(quartic_root(N))/// [L, R] = [1, N]
/// lim = sqrt(N)
ll solve(int p2 = 0, int p3 = 0, int p5 = 0, int p7 = 0, ll P = 1)
{
if (P > lim) return 0; /// Dont care such tuples whose P > sqrt(N)
VL = (L + val - 1) / val; /// ceil(L / P)
VR = R / val; /// floor(R / P)
ll res = magic(0, 18, p2, p3, p5, p7); /// Calculating subproblem
/// By doing these if-condition, it is guarantee that all tuples generated are all unique
if (!p3 && !p5 && !p7) res += solve(p2 + 1, p3, p5, p7, P * 2); /// Continue increasing a
if ( !p5 && !p7) res += solve(p2, p3 + 1, p5, p7, P * 3); /// Continue increasing b
if ( !p7) res += solve(p2, p3, p5 + 1, p7, P * 5); /// Continue increasing c
res += solve(p2, p3, p5, p7 + 1, P * 7); /// Continue increasing d
return res;
}
For each tuples, we need to counting the numbers of such $$$x$$$ that $$$1 \leq x \times f(x) \leq N$$$ and $$$f(x) = P$$$.
- We have the value $$$P$$$, so $$$\lceil \frac{1}{P} \rceil \leq x \leq \lfloor \frac{N}{P} \rfloor$$$.
- We have the value $$$f(x) = P$$$, so $$$x$$$ can be made by digits having the product exactly $$$P$$$, so we can do some DP-digit
So now we have to solve this DP-digit problem: Calculate numbers of such $$$x$$$ ($$$L \leq x \leq R$$$) whose $$$f(x) = P$$$
Solving Subproblem
We try to build each digits by digits for $$$X$$$. Because $$$X \leq N$$$, so we have to build about $$$18$$$ digits.
Lets make a recursive function $$$magic(X, N, p2, p3, p5, p7)$$$
Lets make some definition - $$$VL, VR$$$ means the range of $$$x$$$ needed to get $$$1 \leq x \times f(x) \leq N$$$
- Current prefix of building number is $$$X$$$
- We $$$N$$$ digits left to build to the right of $$$X$$$
- $$$p2, p3, p5, p7$$$ means the left factors in $$$f(x)$$$
- $$$min(X, N) = X * 10^N$$$ means the smallest possible number with prefix $$$X$$$ and $$$N$$$ left digits
- $$$max(X, N) = X * 10^N + 10^N - 1$$$ means the greatest possible number with prefix $$$X$$$ and $$$N$$$ left digits
- $$$cost[v][p]$$$ means greatest divisor $$$p^k$$$ in $$$v$$$ is when $$$k = cost[v][p]$$$
- $$$memo$$$ means current $$$x$$$ is in range $$$[VL, VR]$$$ so we can use memorization
- $$$save = f[N][p2][p3][p5][p7]$$$ means dp-table the numbers of valid number $$$X$$$ with current states
- $$$l_x$$$ means maximum $$$k$$$ that $$$x^k \leq 10^{18}$$$. Where $$$l2, l3, l5, l7, l10$$$ are such those variables we use
- $$$pw10[x] = 10^x$$$
Notice that - Initially, $$$X = 0$$$, $$$N = 18$$$, $$$(p2, p3, p5, p7)$$$ is such tuple generated by above function
- $$$X = 0$$$ means it is not build yet
- $$$VL \leq X \leq VR$$$ means $$$1 \leq x * f(x) \leq N$$$
- $$$N = 0$$$ means $$$X$$$ is built completely
- $$$(p2 + p3 + p5 + p7) = 0$$$ means $$$f(x) = P$$$
- $$$min(X) < VL$$$ means $$$x * f(x) < L$$$ always satisfy which is not desired $$$x$$$ we are considering
- $$$max(X) > VR$$$ means $$$x * f(x) > R$$$ always satisfy which is not desired $$$x$$$ we are considering
- Building such digit $$$v$$$ will reduce each $$$p2, p3, p5, p7$$$ by $$$cost[v][2], cost[v][3], cost[v][5], cost[v][7]$$$
- $$$memo = false$$$ means if we take memorization here, it is not guarantee that $$$1 \leq x * f(x) \leq N$$$
- $$$save = -1$$$ means this dp-state it is not calculated yet
Ugly precalculation code - O(1)const int l2 = 30, l3 = 18, l5 = 12, l7 = 10, l10 = 19;
int pw2[l2], pw3[l3], pw5[l5], pw7[l7]; ll pw10[l10];
int cost[10][10];
void precal()
{
pw2[0] = pw3[0] = pw5[0] = pw7[0] = pw10[0] = 1;
for (int i2 = 1; i2 < l2; ++i2) pw2[i2] = pw2[i2 - 1] * 2;
for (int i3 = 1; i3 < l3; ++i3) pw3[i3] = pw3[i3 - 1] * 3;
for (int i5 = 1; i5 < l5; ++i5) pw5[i5] = pw5[i5 - 1] * 5;
for (int i7 = 1; i7 < l7; ++i7) pw7[i7] = pw7[i7 - 1] * 7;
for (int i = 1; i <l10; ++i ) pw10[i] = pw10[i - 1] * 10;
/// 2357
cost[1][2] = 0; cost[1][3] = 0; cost[1][5] = 0; cost[1][7] = 0; /// 0000 1
cost[2][2] = 1; cost[2][3] = 0; cost[2][5] = 0; cost[2][7] = 0; /// 1000 2
cost[3][2] = 0; cost[3][3] = 1; cost[3][5] = 0; cost[3][7] = 0; /// 0100 3
cost[4][2] = 2; cost[4][3] = 0; cost[4][5] = 0; cost[4][7] = 0; /// 2000 4
cost[5][2] = 0; cost[5][3] = 0; cost[5][5] = 1; cost[5][7] = 0; /// 0010 5
cost[6][2] = 1; cost[6][3] = 1; cost[6][5] = 0; cost[6][7] = 0; /// 1100 6
cost[7][2] = 0; cost[7][3] = 0; cost[7][5] = 0; cost[7][7] = 1; /// 0001 7
cost[8][2] = 3; cost[8][3] = 0; cost[8][5] = 0; cost[8][7] = 0; /// 3000 8
cost[9][2] = 0; cost[9][3] = 2; cost[9][5] = 0; cost[9][7] = 0; /// 0200 9
}
magic function - O(18*p2*p3*p5*p7)ll VL, VR;
ll f[l10][l2][l3][l5][l7];
ll magic(ll X, int N, int p2, int p3, int p5, int p7)
{
if (p2 < 0 || p3 < 0 || p5 < 0 || p7 < 0) return 0;
if (N == 0) return (p2 + p3 + p5 + p7 == 0) && (VL <= X && X <= VR);
ll mn = X * pw10[N];
ll mx = mn + pw10[N] - 1;
if (mx < VL || mn > VR) return 0;
ll &save = f[N][p2][p3][p5][p7];
bool memo = (VL <= mn) && (mx <= VR);
if (memo) if (save != -1) return save;
ll res = 0;
if (X == 0) res = magic(0, N - 1, p2, p3, p5, p7);
for (int v = 1; v <= 9; ++v)
{
int c2 = cost[v][2];
int c3 = cost[v][3];
int c5 = cost[v][5];
int c7 = cost[v][7];
res += magic(X * 10 + v, N - 1, p2 - c2, p3 - c3, p5 - c5, p7 - c7);
}
if (memo) save = res;
return res;
}
About the complexity:
- $$$O(g(x))$$$ is number of such valid tuples, which is approximately $$$O(\sqrt[4]{N})$$$
- $$$O(h(x))$$$ is number of digits we have to build, which is $$$O(log_{10}{N})$$$
- $$$O(k(x))$$$ is product of all prime digits $$$p$$$ with its maximum power $$$k$$$ satisfy $$$p^k \leq MAXN$$$, which is $$$O(log_2(N) \times log_3(N) \times log_5(N) \times log_7(N))$$$
- The space complexity is $$$O(SPACE) = O(h(x) \times k(x))$$$
- The time complexity is $$$O(TIME) = O(SPACE) + O(g(x)) \times k(x))$$$